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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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(' ', ''))
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')
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)