def testInitializationOrder(self): logger = logging.getLogger("pyphant") loghandler = RaiseHandler() logger.addHandler(loghandler) recipe = CompositeWorker.CompositeWorker() recipe.addWorker(DummyConsumer()) recipe.addWorker(DummyProvider()) recipe.getWorker('DummyProvider').paramSource.value = 'The Universe' plug = recipe.getWorker('DummyProvider').getPlug('load') socket = recipe.getWorker('DummyConsumer').getSocket('socket1') socket.insert(plug) handler = H5FileHandler(self.filename, 'w') with handler: handler.saveRecipe(recipe) handler = H5FileHandler(self.filename, 'r') with handler: handler.loadRecipe()
def getH5FileHandler(self, filename, mode='r'): """ Returns an H5FileHandler for the given filename to perform IO operations on the file in a save way. filename -- path to the HDF5 file mode -- see H5FileHandler """ return H5FileHandler(filename, mode)
def main(): km = KnowledgeManager.getInstance() sourcefile = sys.argv[1] h5 = tables.openFile(sourcefile, 'r+') options, args, recipe, orders = processArgs(h5) h5.close() import os.path km.registerURL("file://" + os.path.realpath(sourcefile)) orderLists = [] for order in orders: order = globOrder(order, None) singles = dict( [(socket, id_[0]) \ for socket, id_ in order[0].iteritems() if len(id_) == 1] ) lists = dict( [(socket, id_) \ for socket, id_ in order[0].iteritems() if len(id_) > 1] ) lens = [len(l) for l in lists.itervalues()] assert max(lens) == min(lens), "Illegal lengths." count = lens[0] orderList = [] for i in xrange(count): sockMap = copy.deepcopy(singles) for s, l in lists.iteritems(): sockMap[s] = l[i] orderList.append((sockMap, order[1])) orderLists.append(orderList) orders = sum(orderLists, []) if options.maxOrders == None: count = len(orders) else: count = options.maxOrders i = 0 orderLists = [] while i + count < len(orders): orderLists.append(orders[i: i + count]) i += count orderLists.append(orders[i:]) for i, orderList in enumerate(orderLists): filename = os.path.basename(sourcefile)[:-3] + '_%i.h5' % (i, ) with H5FileHandler(filename, 'w') as handler: handler.saveRecipe(recipe) h5 = tables.openFile(filename, 'r+') for o in orderList: PyTablesPersister.saveExecutionOrder(h5, o) for data in o[0].values(): dc = km.getDataContainer(data) PyTablesPersister.saveResult(dc, h5) h5.close()
def onSaveAsCompositeWorker(self, event=None): msg = "Select file to save recipe." wc = "Pyphant recipe (*.h5)|*.h5" dlg = wx.FileDialog(self, message=msg, defaultDir=os.getcwd(), defaultFile="", wildcard=wc, style=wx.SAVE) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetPath() if not filename.endswith(".h5"): filename += ".h5" with H5FileHandler(filename, 'w') as handler: handler.saveRecipe(self._remainingSpace.diagram.recipe, self._fileMenu.IsChecked(wx.ID_FILE4)) self._wxPyphantApp.pathToRecipe = filename self.recipeState = 'clean' from pyphant.core.WebInterface import shorten self.SetTitle(self.titleStr % shorten(filename, 30, 30)) else: dlg.Destroy()
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import pkg_resources pkg_resources.require('pyphant') pkg_resources.require('pyphant.imageprocessing') # for demo.h5 from pyphant.core.H5FileHandler import H5FileHandler from pyphant.visualizers.ImageVisualizer import ImageVisualizer #Load recipe from hdf file with H5FileHandler('demo.h5', 'r') as handler: recipe = handler.loadRecipe() #Configure ImageLoaderWorker inputWorker = recipe.getWorker('Load Image') imageName = 'demo.png' inputWorker.getParam('filename').value = imageName #Fetch Result worker = recipe.getWorker('Invert') result = worker.plugInvert.getResult() #Visualise result visualizer = ImageVisualizer(result) visualizer.figure.savefig('result-' + imageName)
from pyphant.core.CompositeWorker import CompositeWorker from pyphant.core.H5FileHandler import H5FileHandler # create a recipe recipe = CompositeWorker() # ... fill the recipe (e.g. Listing 1) # save it with H5FileHandler('quicktour_api.h5', 'w') as handler: handler.saveRecipe(recipe, saveResults=True) # load a recipe with H5FileHandler('quicktour_api.h5', 'r') as handler: recipe = handler.loadRecipe()
from pyphant.core.H5FileHandler import H5FileHandler # load the GUI example recipe with H5FileHandler('quicktour.h5', 'r') as handler: recipe = handler.loadRecipe() # access the Gradient worker by name gradient = recipe.getWorker('Gradient')
def onSaveCompositeWorker(self, event=None): with H5FileHandler(self._wxPyphantApp.pathToRecipe, 'w') as handler: handler.saveRecipe(self._remainingSpace.diagram.recipe, self._fileMenu.IsChecked(wx.ID_FILE4)) self.recipeState = 'clean'
def onOpenCompositeWorker(self, event): if not self._wxPyphantApp.pathToRecipe: if pltform == 'Linux' or pltform == 'Darwin': osMessage = "Choose an existing recipe or cancel to create "\ "a new recipe" elif pltform == 'Windows': osMessage = "Choose existing recipe to open or name a new "\ "recipe to create" else: raise OSError, "Operating System %s not supported!" % pltform wc = "Pyphant Recipe(*.h5)|*.h5" dlg = wx.FileDialog(self, message=osMessage, defaultDir=os.getcwd(), defaultFile="", wildcard=wc, style=wx.OPEN) if dlg.ShowModal() == wx.ID_OK: self._wxPyphantApp.pathToRecipe = dlg.GetPath() else: dlg.Destroy() dlg = wx.FileDialog(self, message='Create a new recipe', defaultDir=os.getcwd(), defaultFile="", wildcard=wc, style=wx.SAVE) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() if not path[:-3] == '.h5': path += '.h5' self._wxPyphantApp.pathToRecipe = path else: dlg.Destroy() raise AbortRecipeCreation dlg.Destroy() import PyphantCanvas if self._wxPyphantApp.pathToRecipe[-3:] == '.h5': if os.path.exists(self._wxPyphantApp.pathToRecipe): try: with H5FileHandler(self._wxPyphantApp.pathToRecipe, 'r') \ as handler: recipe = handler.loadRecipe() self._remainingSpace = PyphantCanvas.PyphantCanvas( self, recipe) except: self._wxPyphantApp._logger.error( u"An exception occured while loading a recipe.", exc_info=sys.exc_info()) wx.MessageBox( "An error has occurred while opening "\ "the recipe.\nRecipe has been set to an "\ "empty file in order to prevent data loss.\n"\ "Please investigate the logfile "\ "for further details.\nThe logfile is located at %s"\ % os.path.join(LOGDIR, 'pyphant.log'), "Recipe broken, unknown format or outdated!") self._wxPyphantApp.pathToRecipe += ".error.h5" self._remainingSpace = PyphantCanvas.PyphantCanvas(self) else: self._remainingSpace = PyphantCanvas.PyphantCanvas(self) from pyphant.core.WebInterface import shorten self.SetTitle(self.titleStr \ % shorten(self._wxPyphantApp.pathToRecipe, 30, 30)) else: raise IOError('Unknown file format in file "%s"'\ % self._wxPyphantApp.pathToRecipe) self.recipeState = 'clean' self._remainingSpace.diagram.recipe.registerListener( self.recipeChanged)