def testTableIncludingNanAndErrors(self): X,LAMB = numpy.meshgrid(numpy.linspace(-1.5,1.5,self.n), numpy.linspace(-1.0,1.0,self.m)) self.lambDim = LAMB[:,0] self.xDim = numpy.linspace(-1.5,1.5,self.n) lambField = DC.FieldContainer(self.lambDim, unit = '1 V / m**3', longname='parameter', shortname=r'\lambda') xField = DC.FieldContainer(self.xDim, unit = '1 m', longname = 'position', shortname = 'x') x0,curv,mask = Helpers.fixedPoints(lambField.data,kappa1=self.kappa1) fixedPoints = DC.FieldContainer(numpy.array(x0).transpose(), unit = xField.unit, dimensions=[DC.generateIndex(0,3), lambField], longname = 'position of the local extrema of electric potential', shortname = 'x_0', attributes={'title':'testTableIncludingNanAndErrors'}) fixedPoints.error = 0.1 * fixedPoints.data fixedPoints.seal() visualizer = self.visualizer(fixedPoints,show=False) filename = os.path.join(self.tmpdir,'pyphant-'+DC.parseId(fixedPoints.id)[0]+'%s.%s' % (visualizer.name,outputFormat)) visualizer.figure.savefig(filename.replace(' ',''))
def testDistanceMappingInvertedStringDyDx(self): """ Consider two features, which are divided by a vertical line. The respective distances increase by dx for each column counted from the centre line. """ self.dydx = 0.01 self.yDim = DataContainer.FieldContainer( numpy.linspace(-self.dydx,self.dydx,self.dim), unit = '1m', longname= 'height', shortname='h') referenceField = DataContainer.FieldContainer( numpy.logical_not(stringFeature(self.dim,distanceToBorder=0)), dimensions = [self.xDim, self.yDim], unit = '1', longname='Inverted String Feature', shortname='S') referenceField.seal() result = self.worker.calculateDistanceMap(referenceField) #Compute result afoot afoot = numpy.zeros(referenceField.data.shape,'f') dx = numpy.diff(referenceField.dimensions[0].data)[0] for i in xrange(referenceField.data.shape[1]): afoot[:,i]= dx * abs(5-i) afootC=DataContainer.FieldContainer(afoot, unit=self.xDim.unit, dimensions = map(copy.deepcopy,referenceField.dimensions)) DataContainer.assertEqual(afootC, result)
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 mra(self, field, subscriber=0): dim = field.dimensions[-1] try: scale = quantities.Quantity(self.paramScale.value.encode('utf-8')) except: scale = float(self.paramScale.value) numb_edge = 100.0/self.paramNumb_edge.value d = scipy.diff(dim.data) numpy.testing.assert_array_almost_equal(d.min(), d.max(),4) sigmaMax = scale/(d[0]*dim.unit) if len(field.data.shape)>1: p_e = [] inc = 100./len(field.data) acc = 0. for field1d in field: try: p_e.append(mra1d(dim, field1d, sigmaMax, numb_edge)) except MraError: p_e.append((([],[]),([],[]))) acc += inc subscriber %= acc minima, maxima = zip(*p_e) n_min, pos_min, err_min = pos_error_to_data_container(minima) n_max, pos_max, err_max = pos_error_to_data_container(maxima) dims_min = [DataContainer.generateIndex(0,n_min), field.dimensions[0]] dims_max = [DataContainer.generateIndex(0,n_max), field.dimensions[0]] else: (pos_min, err_min), (pos_max, err_max) = mra1d(dim, field, sigmaMax, numb_edge) dims_min = [DataContainer.generateIndex(0,len(pos_min))] dims_max = [DataContainer.generateIndex(0,len(pos_max))] subscriber %= 100. minima = DataContainer.FieldContainer(pos_min.transpose(), error = err_min.transpose(), unit = dim.unit, dimensions = dims_min, mask = numpy.isnan(pos_min).transpose(), longname="%s of the local %s of %s" % (dim.longname,"minima",field.longname), shortname="%s_{min}" % dim.shortname) maxima = DataContainer.FieldContainer(pos_max.transpose(), error = err_max.transpose(), unit = dim.unit, dimensions = dims_max, mask = numpy.isnan(pos_max).transpose(), longname="%s of the local %s of %s" % (dim.longname,"maxima",field.longname), shortname="%s_{max}" % dim.shortname) roots = DataContainer.SampleContainer([minima, maxima], longname="%s of the local %s of %s" % (dim.longname,"extrema",field.longname), shortname="%s_{extrem}" % dim.shortname) if self.paramLongname.value != 'default': roots.longname = self.paramLongname.value if self.paramSymbol.value != 'default': roots.shortname = self.paramSymbol.value roots.seal() return roots
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 loadSample(h5, resNode): result = DataContainer.SampleContainer.__new__( DataContainer.SampleContainer ) result.longname = unicode(h5.getNodeAttr(resNode, "longname"), 'utf-8') result.shortname = unicode(h5.getNodeAttr(resNode, "shortname"), 'utf-8') result.creator = unicode(h5.getNodeAttr(resNode, "creator"), 'utf-8') result.machine = unicode(h5.getNodeAttr(resNode, "machine"), 'utf-8') result.attributes = {} for key in resNode._v_attrs._v_attrnamesuser: if key not in _reservedAttributes: result.attributes[key] = h5.getNodeAttr(resNode, key) columns = [] for resId in h5.getNodeAttr(resNode, "columns"): nodename = "/results/" + resId hash, uriType = DataContainer.parseId( h5.getNodeAttr(nodename, "TITLE") ) if uriType == 'sample': loader = loadSample elif uriType == 'field': loader = loadField else: raise KeyError( "Unknown UriType %s in saving result %s." % ( uriType, result.id ) ) columns.append(loader(h5, h5.getNode(nodename))) result.columns = columns result.seal(resNode._v_title) return result
def testIntersectionXVector(self): X,LAMB = numpy.meshgrid(numpy.linspace(-1.5,1.5,self.n), numpy.linspace(-1.0,1.0,self.m)) self.lambDim = LAMB[:,0] self.xDim = numpy.linspace(-1.5,1.5,self.n) lambField = DC.FieldContainer(self.lambDim, unit = '1 V / m**3', longname='parameter', shortname=r'\lambda') xField = DC.FieldContainer(self.xDim, unit = '1 m', longname = 'position', shortname = 'x') #Prepare potential V = [] for i in xrange(len(lambField.data)): u = X[i] 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', attributes={'title':'testIntersectionXVector'}) self.V.seal() visualizer = self.visualizer(self.V,show=False) filename = os.path.join(self.tmpdir,'pyphant-'+DC.parseId(self.V.id)[0]+'%s.%s' % (visualizer.name,outputFormat)) visualizer.figure.savefig(filename.replace(' ',''))
def saveDataContainer(self, result): """ Saves a given DataContainer instance to the HDF5 file. The DataContainer has to be sealed or at least provide a valid emd5 in its '.id' attribute. A HDF5 group path that points to the location the DC was stored at is returned. result -- sealed DC instance """ dcHash, uriType = DataContainer.parseId(result.id) resId = u"result_" + dcHash try: resultGroup = self.handle.getNode("/results/" + resId) except tables.NoSuchNodeError: try: resultGroup = self.handle.createGroup( "/results", resId, result.id.encode("utf-8") ) except tables.NoSuchNodeError: self.handle.createGroup('/', 'results') resultGroup = self.handle.createGroup( "/results", resId, result.id.encode("utf-8") ) if uriType == 'field': self.saveField(resultGroup, result) elif uriType == 'sample': self.saveSample(resultGroup, result) else: raise KeyError( "Unknown UriType %s in saving result %s." % ( uriType, result.id ) ) return resId
def testVisualization(self): X,LAMB = numpy.meshgrid(numpy.linspace(-1.5,1.5,self.n), numpy.linspace(-1.0,1.0,self.m)) self.lambDim = numpy.linspace(-1.0,1.0,self.m) self.xDim = numpy.linspace(-1.5,1.5,self.n) lambField = DC.FieldContainer(self.lambDim, unit = '1 V / m**3', longname='parameter', shortname='\lambda') xField = DC.FieldContainer(self.xDim, unit = '1 m', longname = 'position', shortname = 'x') #Prepare potential V = [] for i in xrange(len(lambField.data)): u = X[i] 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') self.V.seal() #Visualise result visualizer = ImageVisualizer(self.V,show=False) filename = os.path.join(self.tmpdir,'pyphant-'+DC.parseId(self.V.id)[0]+'.pdf') visualizer.figure.savefig(filename)
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 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 loadField(h5, resNode): longname = unicode(h5.getNodeAttr(resNode, "longname"), 'utf-8') shortname = unicode(h5.getNodeAttr(resNode, "shortname"), 'utf-8') try: creator = unicode(h5.getNodeAttr(resNode, "creator"), 'utf-8') machine = unicode(h5.getNodeAttr(resNode, "machine"), 'utf-8') except: from pyphant.core.Helpers import emd52dict emd5dict = emd52dict(resNode._v_title) creator = emd5dict['creator'] machine = emd5dict['machine'] data = scipy.array(resNode.data.read()) def loads(inputList): if type(inputList) == type([]): try: return map(lambda s: eval(s), inputList) except: return map(lambda s: unicode(s, 'utf-8'), inputList) else: return map(loads, inputList) if data.dtype.char == 'S': data = scipy.array(loads(data.tolist())) attributes = {} for key in resNode.data._v_attrs._v_attrnamesuser: attributes[key] = h5.getNodeAttr(resNode.data, key) try: error = scipy.array(resNode.error.read()) except tables.NoSuchNodeError: error = None try: mask = scipy.array(resNode.mask.read()) except tables.NoSuchNodeError: mask = None unit = eval(unicode(h5.getNodeAttr(resNode, "unit"), 'utf-8')) try: dimTable = resNode.dimensions dimensions = [ loadField( h5, h5.getNode( "/results/result_" + DataContainer.parseId(row['id'])[0] ) ) for row in dimTable.iterrows() ] except tables.NoSuchNodeError: dimensions = DataContainer.INDEX result = DataContainer.FieldContainer(data, unit, error, mask, dimensions, longname, shortname, attributes) result.creator = creator result.machine = machine result.seal(resNode._v_title) return result
def testSkeletonizeWideFeature(self): """The skeleton of a feature with width three is the small feature tested in the foregoing test.""" referenceField = DataContainer.FieldContainer( self.wideFeature.copy(), unit='1', longname='Simple Feature', shortname='S') referenceField.seal() result = self.worker.execute(referenceField) numpy.testing.assert_array_equal(self.feature, result.data) self.assertEqual(result.unit, referenceField.unit)
def testDistanceMappingVerticalString(self): """ All elements of a perpendicular string-like feature have distance dx to the background. """ referenceField = DataContainer.FieldContainer( stringFeature(self.dim, distanceToBorder=self.dist), dimensions=[self.xDim, self.yDim], unit='1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.calculateDistanceMap(referenceField) #Compute result afoot afoot = DataContainer.FieldContainer( numpy.where(referenceField.data == I.FEATURE_COLOR, self.dx, 0), dimensions=map(copy.deepcopy, [self.xDim, self.yDim]), unit=self.xDim.unit, rescale=True) self.assertEqual(afoot, result)
def testThinCurvedFeature(self): """A curved feature of one pixel width should not be altered due to the Skeletonization algorithm.""" referenceField = DataContainer.FieldContainer( self.quarterRing, unit = '1', longname='Curved Feature', shortname='S') referenceField.seal() result = self.worker.execute(referenceField) numpy.testing.assert_array_equal(self.quarterRing, result.data) self.assertEqual(result.unit,referenceField.unit)
def testVisualization(self): X = numpy.linspace(-1.5, 1.5, self.n) self.lambDim = 1.0 xField = DC.FieldContainer(X, unit='1 m', longname='position', shortname='x') self.V = DC.FieldContainer(-self.lambDim / 2 * X**2 + X**4 / 4 - X * self.kappa1, unit='1 V', dimensions=[xField], longname='electric potential', shortname=r'\varphi', attributes={'title': 'testVisualization'}) self.V.seal() visualizer = self.visualizer(self.V, show=False) filename = os.path.join( self.tmpdir, 'pyphant-' + DC.parseId(self.V.id)[0] + '%s.%s' % (visualizer.name, outputFormat)) visualizer.figure.savefig(filename.replace(' ', ''))
def testSkeletonizeThinFeature(self): """A feature of width one is already the skeleton and should not be modified.""" referenceField = DataContainer.FieldContainer( self.feature.copy(), unit = '1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.execute(referenceField) numpy.testing.assert_array_equal(self.feature,result.data) self.assertEqual(result.unit,referenceField.unit)
def loadImageAsGreyScale( filename, xscale='1mum', yscale='link2X', fieldunit=1 ): from PIL import Image import scipy from scipy.misc import fromimage from pyphant.core import DataContainer from pyphant.quantities import Quantity im = Image.open(filename) if im.mode == "I;16": im = im.convert("I") data = fromimage(im).astype("int16") else: data = fromimage(im, flatten=True) Ny, Nx = data.shape xUnit = Quantity(xscale.encode('utf-8')) xAxis = DataContainer.FieldContainer(scipy.linspace(0.0, xUnit.value, Nx, True), xUnit / xUnit.value, longname = 'x-coordinate', shortname = 'x') if yscale == 'link2X': yUnit = xUnit * float(Ny) / Nx else: yUnit = Quantity(yscale.encode('utf-8')) yAxis = DataContainer.FieldContainer(scipy.linspace(0.0, yUnit.value, Ny, True), yUnit / yUnit.value, longname = 'y-coordinate', shortname = 'y') try: FieldUnit = Quantity(fieldunit.encode('utf-8')) except AttributeError: FieldUnit = fieldunit return DataContainer.FieldContainer( data, FieldUnit, longname="Image", shortname="I", dimensions=[yAxis, xAxis] )
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)
def threshold(self, image, subscriber=0): th = self.paramThreshold.value resultArray = scipy.where(image.data < th, ImageProcessing.FEATURE_COLOR, ImageProcessing.BACKGROUND_COLOR) result = DataContainer.FieldContainer(resultArray, dimensions=copy.deepcopy( image.dimensions), longname=u"Binary Image", shortname=u"B") result.seal() return result
def testNegligibleNoise(self): """Tests the merging of abscissae data in case of negliglible deviation.""" worker = OA.OscAbsorptionCalculator() worker.paramClipping.value = 0 self.sampleC['I'].dimensions[-1].data += 1e-8 * numpy.random.randn( self.n) self.sampleC.seal() result = worker.calcAbsorption(self.sampleC) expectedDim = [ DC.generateIndex(1, self.m), DC.FieldContainer(self.x, longname='position', shortname='x', unit='1m') ] expectedResult = DC.FieldContainer(numpy.ones( (self.m, self.n), 'float') - self.I.data, dimensions=expectedDim, longname=u'absorption', shortname=ur'\tilde{A}') self.assertEqual(result, expectedResult)
def calcSmoother(self, osc, subscriber=0): x,y = copy.deepcopy(osc.dimensions[0].data),copy.deepcopy(osc.dimensions[1].data) X,Y = scipy.meshgrid(x,y) Z = osc.data#scipy.diff(osc.data,2,0) spline = scipy.interpolate.interp2d(X.flatten(),Y.flatten(),Z.flatten()) X,Y = scipy.meshgrid(numpy.linspace(x.min(), x.max(), 10), numpy.linspace(y.min(), y.max(), 10)) Z = spline(X.flatten(),Y.flatten()) Z.resize((10,10)) result = DataContainer.FieldContainer(Z,dimensions=osc.dimensions) result.seal() return result
def testCalculation(self): """Tests the correct calculation of absorption without clipping. It is assumed, that the dark reference intensity is equal to zero, while white reference intensity is equal to one.""" worker = OA.OscAbsorptionCalculator() worker.paramClipping.value = 0 self.sampleC.seal() result = worker.calcAbsorption(self.sampleC) expectedDim = [ DC.generateIndex(1, self.m), DC.FieldContainer(self.x, longname='position', shortname='x', unit='1m') ] expectedResult = DC.FieldContainer(numpy.ones( (self.m, self.n), 'float') - self.I.data, dimensions=expectedDim, longname=u'absorption', shortname=ur'\tilde{A}') self.assertEqual(result, expectedResult)
def documentCovering(self, image, subscriber=0): thresholds = scipy.array([ self.paramLowerThreshold.value, self.paramUpperThreshold.value, scipy.amax(image.data) ]) coveringVec = self.getCovering(image) theta = DataContainer.FieldContainer( thresholds, '1', longname='Value of upper threshold', shortname='\theta') A = DataContainer.FieldContainer(coveringVec, '1', longname='Covering', shortname='A') print theta.data, thresholds print A.data, coveringVec res = DataContainer.SampleContainer([theta, A], u"Covering of image parts", u"X_A") res.seal() return res
def testDistanceMappingHorizontalString(self): """ The elements of a horizontal string-like feature spanning the whole domain have distance dy/dx to the background. """ referenceField = DataContainer.FieldContainer( numpy.rot90(stringFeature(self.dim, distanceToBorder=self.dist)), dimensions=[self.xDim, self.yDim], unit='1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.calculateDistanceMap(referenceField) #Compute result afoot #Compute result afoot afoot = DataContainer.FieldContainer( numpy.where(referenceField.data == I.FEATURE_COLOR, self.dy, 0), dimensions=map(copy.deepcopy, [self.xDim, self.yDim]), unit=self.xDim.unit, rescale=True) self.assertEqual(afoot, result)
def setUp(self): self.n = 100 self.m = 10 self.kappa1 = 0.0 self.errLevelPos = 6 self.errLevelCurv = 5 self.x = numpy.linspace(-1.5, 1.5, self.n) self.lamb = numpy.linspace(-1.0, 1.0, self.m) X, LAMB = scipy.meshgrid(self.x, self.lamb) lambField = DC.FieldContainer(LAMB, unit='1 V / m**3', longname='parameter', shortname='\lambda') xField = DC.FieldContainer(X[0], unit='1 m', longname='position', shortname='x') 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.I = DC.FieldContainer(numpy.array(V), longname='intensity', shortname='I') self.I.dimensions[-1] = xField self.I0 = DC.FieldContainer(numpy.ones(self.I.data.shape, 'float'), longname='white reference', shortname='I_0') self.I0.dimensions[-1] = xField self.Id = DC.FieldContainer(numpy.zeros(self.I.data.shape, 'float'), longname='darf reference', shortname='I_d') self.Id.dimensions[-1] = xField self.sampleC = DC.SampleContainer([self.I, self.I0, self.Id])
def testSlopeOfString(self): """All elements of a string-like feature have distance 1 to the background.""" referenceField = DataContainer.FieldContainer( stringFeature(self.dim), unit='1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.slope(referenceField) afoot = numpy.where(referenceField.data == I.FEATURE_COLOR, 1, 0)
def saveResult(result, h5): hash, uriType = DataContainer.parseId(result.id) resId = u"result_"+hash try: resultGroup = h5.getNode("/results/"+resId) except tables.NoSuchNodeError, e: resultGroup = h5.createGroup("/results", resId, result.id.encode("utf-8")) if uriType=='field': saveField(h5, resultGroup, result) elif uriType=='sample': saveSample(h5, resultGroup, result) else: raise KeyError, "Unknown UriType %s in saving result %s." % (uriType, result.id)
def testPitchfork(self): referenceField = DataContainer.FieldContainer( self.pf, unit='1', longname='Inverted String Feature', shortname='S') referenceField.seal() result = self.worker.calculateDistanceMap(referenceField) #Compute result afoot dx = referenceField.dimensions[0].data[1] - referenceField.dimensions[ 0].data[0] dy = referenceField.dimensions[1].data[1] - referenceField.dimensions[ 1].data[0] rt = numpy.sqrt(dx**2 + dy**2) afoot = numpy.array([[2 * rt, 2 * dx, dx, 0], [rt + dx, rt, dy, dy], [2 * dx, dx, 0, dx], [dy, dy, dy, dy], [0, 0, 0, 0], [dy, dy, dy, dy], [2 * dx, dx, 0, dx], [rt + dx, rt, dy, dy], [2 * rt, 2 * dx, dx, 0]]) afoot = DataContainer.FieldContainer( afoot / dx, unit=referenceField.dimensions[0].unit, rescale=True) self.assertEqual(afoot, result)
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) x0 = numpy.where(curv > 0, x0, numpy.NaN) data = x0[:, ::2] dims = [DC.generateIndex(0, 2), lambField] expectedResult = DC.FieldContainer( data.transpose(), unit=xField.unit, mask=numpy.isnan(data).transpose(), dimensions=dims, longname='position of the local extrema of electric potential', shortname='x_0' ) #Configure worker w = MRA.MRA(None) w.paramScale.value = "1.0m" #Retrieve result from worker result = copy.deepcopy(w.mra(self.V))['x_{min}'] result.error=None self.test(result,expectedResult,1e-2,1e-2)
def computeDistances(self, field, subscriber=1, percentage=0): xGrid,yGrid = numpy.meshgrid(field.dimensions[-1].data,field.dimensions[-2].data) x = numpy.extract(numpy.logical_not(numpy.isnan(field.data)),xGrid) xCon = DataContainer.FieldContainer(x,unit=field.dimensions[-1].unit, longname=field.dimensions[-1].longname, shortname=field.dimensions[-1].shortname) y = numpy.extract(numpy.logical_not(numpy.isnan(field.data)),field.data) yCon = DataContainer.FieldContainer(y,longname=field.longname, shortname=field.shortname, unit=field.unit) xOff, xStep, xInd = OA.grid2Index(x, self.paramExtentX.value) yOff, yStep, yInd = OA.grid2Index(y, self.paramExtentY.value) xMax = xInd.maxV yMax = yInd.maxV xDim = DataContainer.FieldContainer( numpy.linspace(xInd.minV,xInd.maxV,xInd.stepCount), xCon.unit, longname = xCon.longname, shortname = xCon.shortname ) yDim = DataContainer.FieldContainer( numpy.linspace(yInd.minV,yInd.maxV,yInd.stepCount), yCon.unit, longname = yCon.longname, shortname = yCon.shortname ) functional = numpy.ones((xInd.stepCount, yInd.stepCount), dtype='float') distances = numpy.zeros(x.shape,'f') ni = functional.shape[0] nj = functional.shape[1] increment = 50.0/(ni*nj) for i in xrange(ni): for j in xrange(nj): for k in xrange(len(x)): distances[k] = numpy.sqrt((x[k]-xDim.data[i])**2+ (y[k]-yDim.data[j])**2) functional[i,j]=distances.min() percentage += increment subscriber %= percentage result = DataContainer.FieldContainer(functional.transpose(), dimensions=[yDim, xDim], longname = 'functional of %s'%field.longname, shortname= 'F_{%s}'%field.shortname ) return result
def calcNormal(self, osc, xCon, yCon, fCon, xf, yf, h): xOff, xStep, xInd = grid2Index(xf, self.paramExtentX.value) yOff, yStep, yInd = grid2Index(yf, self.paramExtentY.value) xMax = xInd.maxV # never used! yMax = yInd.maxV # never used! xDim = DataContainer.FieldContainer( numpy.linspace(xInd.minV, xInd.maxV, xInd.stepCount) - 0.5 * xStep, xCon.unit, longname = xCon.longname, shortname = xCon.shortname ) yDim = DataContainer.FieldContainer( numpy.linspace(yInd.minV, yInd.maxV, yInd.stepCount) - 0.5 * yStep, yCon.unit, longname = yCon.longname, shortname = yCon.shortname ) img = numpy.ones((yInd.stepCount, xInd.stepCount), dtype='float') * numpy.NaN mask = numpy.ones((yInd.stepCount, xInd.stepCount), dtype='bool') for i in xrange(xf.size): xi = xInd[xf[i]] yi = yInd[yf[i]] if not mask[yi, xi]: self._logger.warning("Duplicate data for pixel (%.4g,%.4g). " "Using first found value. " "Is your data corrupt?"%(xf[i],yf[i])) else: img[yi, xi] = h[i] if h[i] > 0: mask[yi, xi] = False result = DataContainer.FieldContainer( img, fCon.unit, mask=mask, dimensions=[yDim, xDim], longname=u'Map of %s'%fCon.longname, shortname=fCon.shortname ) return result
def testNegligibleNoise(self): """Tests the merging of abscissae data in case of negliglible deviation.""" worker = OA.OscAbsorptionCalculator() worker.paramClipping.value = 0 self.sampleC['I'].dimensions[-1].data += 1e-8*numpy.random.randn(self.n) self.sampleC.seal() result = worker.calcAbsorption(self.sampleC) expectedDim = [DC.generateIndex(1,self.m), DC.FieldContainer(self.x,longname='position',shortname='x',unit='1m')] expectedResult = DC.FieldContainer(numpy.ones((self.m,self.n),'float')-self.I.data, dimensions=expectedDim, longname=u'absorption', shortname=ur'\tilde{A}') self.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 testBlockSkeletonize(self): """The skeleton of a simple 3x3 block is its centre pixel.""" feature = numpy.zeros((11, 11)) feature[:, :] = I.BACKGROUND_COLOR skeleton = feature.copy() feature[1:4, 4:7] = I.FEATURE_COLOR skeleton[2, 5] = I.FEATURE_COLOR referenceField = DataContainer.FieldContainer( feature, unit='1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.execute(referenceField) numpy.testing.assert_array_equal(skeleton, result.data) self.assertEqual(result.unit, referenceField.unit)
def testSkeletonize(self): """Two 3x3 blocks of features, which are connected by a thin string of features, lead to a skeleton, which is s simple line.""" feature = self.skeleton.copy() feature[1:4,4:7] = I.FEATURE_COLOR feature[7:10,4:7]= I.FEATURE_COLOR referenceField = DataContainer.FieldContainer( feature, unit = '1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.execute(referenceField) numpy.testing.assert_array_equal(self.skeleton,result.data) self.assertEqual(result.unit,referenceField.unit)
def testSignificantNoise(self): """Tests the conservation of abscissae data in case of significant deviation.""" worker = OA.OscAbsorptionCalculator() worker.paramClipping.value = 0 self.sampleC['I'].dimensions[-1].data += numpy.random.randn(self.n) self.sampleC.seal() result = worker.calcAbsorption(self.sampleC) expectedDim = copy.deepcopy(self.sampleC['I'].dimensions) expectedResult = DC.FieldContainer(numpy.ones( (self.m, self.n), 'float') - self.I.data, dimensions=expectedDim, longname=u'absorption', shortname=ur'\tilde{A}') self.assertEqual(result, expectedResult)
def compute(self, model, experimental, subscriber=1): minima_model = model[self.paramMinima_model.value] maxima_model = model[self.paramMaxima_model.value] minima_experimental = experimental[ self.paramMinima_experimental.value].inUnitsOf( minima_model.dimensions[0]) maxima_experimental = experimental[ self.paramMaxima_experimental.value].inUnitsOf( maxima_model.dimensions[0]) minima = minima_experimental.data.transpose() if minima_experimental.error != None: minima_error = iter(minima_experimental.error.transpose()) else: minima_error = None maxima = maxima_experimental.data.transpose() if maxima_experimental.error != None: maxima_error = iter(maxima_experimental.error.transpose()) else: maxima_error = None parameter = [] inc = 100.0 / float(len(minima)) acc = inc subscriber %= acc for row_minima, row_maxima in zip(minima, maxima): if minima_error: filtered_minima_error = filter(lambda c: not numpy.isnan(c), minima_error.next()) else: filtered_minima_error = None if maxima_error: filtered_maxima_error = filter(lambda c: not numpy.isnan(c), maxima_error.next()) else: filtered_maxima_error = None parameter.append( self.calculateThickness( filter(lambda c: not numpy.isnan(c), row_minima), filter(lambda c: not numpy.isnan(c), row_maxima), minima_model, maxima_model, filtered_minima_error, filtered_maxima_error)) acc += inc subscriber %= acc result = DataContainer.FieldContainer( numpy.array(parameter), longname=minima_model.dimensions[-1].longname, shortname=minima_model.dimensions[-1].shortname, unit=minima_model.dimensions[-1].unit) result.seal() return result
def saveResult(result, h5): hash, uriType = DataContainer.parseId(result.id) resId = u"result_" + hash try: resultGroup = h5.getNode("/results/" + resId) except tables.NoSuchNodeError, e: resultGroup = h5.createGroup("/results", resId, result.id.encode("utf-8")) if uriType == 'field': saveField(h5, resultGroup, result) elif uriType == 'sample': saveSample(h5, resultGroup, result) else: raise KeyError, "Unknown UriType %s in saving result %s." % ( uriType, result.id)
def execute(self): dialog = wx.FileDialog(None,message='Choose file for saving the data', defaultDir=os.getcwd(), style=wx.SAVE | wx.OVERWRITE_PROMPT, wildcard = "Comma separated values (*.csv)|*.csv|Plain text (*.dat)|*.dat") if dialog.ShowModal() == wx.ID_OK: path = dialog.GetPath() print "Selected:",path else: print "Nothing was selected." dialog.Destroy() hash, uriType = DataContainer.parseId(self.dataContainer.id) if uriType == u"field": self.saveField(path) elif uriType == u"sample": self.saveSample(path)
def testDistanceMappingDoubleString(self): """ All elements of a string-like feature of width 2 have distance 1 to the background. """ referenceField = DataContainer.FieldContainer( stringFeature(self.dim, width=2, distanceToBorder=self.dist), unit='1', longname='String Feature', shortname='S') referenceField.seal() result = self.worker.calculateDistanceMap(referenceField) #Compute result afoot afoot = numpy.where(referenceField.data == I.FEATURE_COLOR, 1.0, 0) numpy.testing.assert_array_equal(afoot, result.data)
def testDifferentDimensionLength(self): """ No exception should be raised if the discretisation is correct, but the length of the dimension vectors differ. """ defaultDim = DataContainer.FieldContainer(numpy.linspace(1, 11, 11)) exceptDim = DataContainer.FieldContainer(numpy.linspace(1, 10, 10)) field = numpy.fromfunction(lambda i, j: i, [11, 10]) referenceField = DataContainer.FieldContainer( field, unit='1', dimensions=[defaultDim, exceptDim], longname='String Feature', shortname='S') referenceField.seal() try: result = self.worker.calculateDistanceMap(referenceField) except ValueError, e: self.failIf( True, "No ValueError should be raised, if the discretisation" + " of two dimension vectors is correct, but the lenght" + " of the vectors differ.")
def getNodeAndTypeFromId(self, dcId): """ Returns a tuple (HDF5 node, uriType) for the given DataContainer emd5. dcId -- emd5 of the DataContainer """ dcHash, uriType = DataContainer.parseId(dcId) try: resNode = self.handle.getNode("/results/result_" + dcHash) except (AttributeError, tables.NoSuchNodeError): raise AttributeError("Container %s not found in file %s." % (dcId, self.filename)) if isinstance(uriType, unicode): uriType = uriType.encode('utf-8') return (resNode, uriType)
def testCalculation(self): """Tests the correct calculation of absorption without clipping. It is assumed, that the dark reference intensity is equal to zero, while white reference intensity is equal to one.""" worker = OA.OscAbsorptionCalculator() worker.paramClipping.value = 0 self.sampleC.seal() result = worker.calcAbsorption(self.sampleC) expectedDim = [DC.generateIndex(1,self.m), DC.FieldContainer(self.x,longname='position',shortname='x',unit='1m')] expectedResult = DC.FieldContainer(numpy.ones((self.m,self.n),'float')-self.I.data, dimensions=expectedDim, longname=u'absorption', shortname=ur'\tilde{A}') self.assertEqual(result,expectedResult)
def enhance(self, image, subscriber=0): newdata = normalizeLinear(image.data) longname = "Normalize" result = DataContainer.FieldContainer( newdata, 1, None, copy.deepcopy(image.mask), copy.deepcopy(image.dimensions), longname, image.shortname, copy.deepcopy(image.attributes), False) result.seal() return result
def wsworker(self, image, markers, subscriber=0): self._markers = markers.data newdata = self.watershed(image.data) longname = "Watershed" result = DataContainer.FieldContainer(newdata, copy.deepcopy(image.unit), copy.deepcopy(image.error), copy.deepcopy(image.mask), copy.deepcopy(image.dimensions), longname, image.shortname, copy.deepcopy(image.attributes), False) result.seal() #print newdata.shape return result
def extract(self, osc, subscriber=0): col = osc[self.paramColumn.value] if self.paramIndex.value=='All': result = copy.deepcopy(col) else: index = int(self.paramIndex.value) if len(col.dimensions)>1: dim = col.dimensions[1] else: oldDim = col.dimensions[0] dim = DataContainer.FieldContainer(oldDim.data[index], unit = oldDim.unit, longname=oldDim.longname, shortname=oldDim.shortname) data = col.maskedData[index] result = DataContainer.FieldContainer(data.data, mask=data.mask, unit = col.unit, dimensions = [dim], longname=col.longname, shortname=col.shortname) #result.attributes = osc.attributes result.attributes = col.attributes result.seal() return result
def testVisualization(self): X = numpy.linspace(-1.5,1.5,self.n) self.lambDim = 1.0 xField = DC.FieldContainer(X, unit = '1 m', longname = 'position', shortname = 'x') self.V = DC.FieldContainer(-self.lambDim/2* X**2 + X**4/4-X*self.kappa1, unit='1 V',dimensions=[xField], longname = 'electric potential', shortname=r'\varphi', attributes={'title':'testVisualization'}) self.V.seal() visualizer = self.visualizer(self.V,show=False) filename = os.path.join(self.tmpdir,'pyphant-'+DC.parseId(self.V.id)[0]+'%s.%s' % (visualizer.name,outputFormat)) visualizer.figure.savefig(filename.replace(' ',''))
def restoreResultsToWorkers(recipeGroup, workers, h5): for workerGroup in recipeGroup: for plugGroup in workerGroup.plugs: plug=workers[workerGroup._v_name].getPlug(plugGroup._v_name) try: resId = plugGroup._v_attrs.result resNode = h5.getNode("/results/"+resId) hash, uriType = DataContainer.parseId(resNode._v_title) if uriType==u'field': result=loadField(h5, resNode) elif uriType==u'sample': _logger.info("Trying to load sample data...") result=loadSample(h5, resNode) _logger.info("...successfully loaded.") else: raise TypeError, "Unknown result uriType in <%s>"%resNode._v_title plug._result = result except (AttributeError, tables.NoSuchNodeError), e: _logger.info( "Exception: "+str(e) )
def extract(self, field, subscriber=0): if not hasattr(self, 'paramDim0'): self.refreshParams() params = [str(eval('self.paramDim%i.value' %i)) for i in range(len(field.dimensions))] for dim, arg in enumerate(params): if arg.startswith('#'): step = None if arg == '#:': start = 0 end = len(field.dimensions[dim].data) elif arg[1] == ':': start = 0 end = long(arg[2:]) + 1 elif arg[-1] == ':': start = long(arg[1: -1]) end = len(field.dimensions[dim].data) else: ind = map(long, arg[1:].split(':')) start = ind[0] if len(ind) == 1: end = ind[0] + 1 elif len(ind) >= 2: end = ind[1] + 1 if len(ind) == 3: step = ind[2] if len(ind) > 3: raise ValueError("Illegal slice with " "more than two colons.") params[dim] = slice(start, end, step) else: s = DataContainer.slice2ind(arg, field.dimensions[dim]) params[dim] = slice(s.start, min(s.stop + 1, len(field.dimensions[dim].data)), s.step) result = copy.deepcopy(field[params]) result.seal() return result
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)
data = scipy.array(loads(data.tolist())) attributes = {} for key in resNode.data._v_attrs._v_attrnamesuser: attributes[key]=h5.getNodeAttr(resNode.data,key) try: error = scipy.array(resNode.error.read()) except tables.NoSuchNodeError, e: error = None try: mask = scipy.array(resNode.mask.read()) except tables.NoSuchNodeError, e: mask = None unit = eval(unicode(h5.getNodeAttr(resNode, "unit"), 'utf-8')) try: dimTable = resNode.dimensions dimensions = [loadField(h5, h5.getNode("/results/result_"+DataContainer.parseId(row['id'])[0])) for row in dimTable.iterrows()] except tables.NoSuchNodeError, e: dimensions = DataContainer.INDEX result = DataContainer.FieldContainer(data, unit, error, mask, dimensions, longname, shortname, attributes) result.creator = creator result.machine = machine result.seal(resNode._v_title) return result def loadSample(h5, resNode): result = DataContainer.SampleContainer.__new__(DataContainer.SampleContainer) result.longname = unicode(h5.getNodeAttr(resNode, "longname"), 'utf-8') result.shortname = unicode(h5.getNodeAttr(resNode, "shortname"), 'utf-8')