def runScript(session, scriptService, scriptPath): # Identify the script we want to run: Get all 'my' scripts and filter by path. acceptsList = [] # An empty list implies that the server should return what it would by default trust. scripts = scriptService.getUserScripts(acceptsList) # returns list of OriginalFiles for s in scripts: print s.id.val, s.path.val + s.name.val namedScripts = [s.id.val for s in scripts if s.path.val + s.name.val == scriptPath] if len(namedScripts) == 0: print "Didn't find any scripts with specified path" # use the most recent script (highest ID) scriptId = max(namedScripts) print "Running script: %s with ID: %s" % (scriptPath, scriptId) # Don't attempt to run script without starting user processor! # return # make a map of all the parameters we want to pass to the script # keys are strings. Values must be omero.rtypes such as rlong, rbool, rlist. map = { "Input_Message": omero.rtypes.rstring("Sending this message to the server!"), } # The last parameter is how long to wait as an RInt proc = scriptService.runScript(scriptId, map, None) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) # handle any results from the script #print results.keys() if 'Message' in results: print results['Message'].getValue() rawFileService = session.createRawFileStore() queryService = session.getQueryService() if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdOut in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdErr in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) rawFileService.close()
def getSpfFiles(queryService, rawFileService): """ list original files ending with .spf and get text for each, but provide the annotation ID (same as Insight) returns a list of (id, name, text)""" query = "select a from Annotation a join fetch a.file as f where f.name like '%.spf'" annotations = queryService.findAllByQuery(query, None) spfFiles = [] for a in annotations: aId = a.getId().getValue() fileName = a.file.getName().getValue() fileId = a.file.getId().getValue() text = scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) spfFiles.append({"id": aId, "name": fileName, "text": text}) return spfFiles
def runScript(session, scriptService, scriptPath): """ This will attempt to run the script (scriptPath can be path or ID), asking the user for inputs for all the script parameters, and printing out the results when the script completes. """ try: scriptId = long(scriptPath) except: scriptId = scriptService.getScriptID(scriptPath) # Identify the script we want to run: Get all 'official' scripts and filter by path. print "Running script: %s with ID: %s" % (scriptPath, scriptId) # make a map of all the parameters we want to pass to the script # keys are strings. Values must be omero.rtypes such as rlong, rbool, rlist. map = {} params = scriptService.getParams(scriptId) for key, param in params.inputs.items(): print "" print key if param.description: print param.description if not param.optional: print " * Required" if param.values: print "Options:", ", ".join(unwrap(param.values)) if param.min: print "Min:", param.min.getValue() if param.max: print "Max:", param.max.getValue() prototype = param.prototype prompt = ": " default = None if param.useDefault: default = param.prototype.val prompt = "[%s]: " % default pclass = prototype.__class__ if pclass == omero.rtypes.RListI: valueList = [] listClass = omero.rtypes.rstring l = prototype.val # list if len(l) > 0: # check if a value type has been set (first item of prototype list) listClass = l[0].getValue().__class__ if listClass == int(1).__class__: listClass = omero.rtypes.rint if listClass == long(1).__class__: listClass = omero.rtypes.rlong print "List:" while(True): value = raw_input(prompt) if value == "": break try: obj = listClass(value) except: print "Invalid entry" continue if isinstance(obj, omero.model.IObject): valueList.append(omero.rtypes.robject(obj)) else: valueList.append(obj) if len(valueList) > 0: map[key] = omero.rtypes.rlist(valueList) elif pclass == omero.rtypes.RMapI: print "MAP!" valueMap = {} m = prototype.val # check if a value type has been set for the map print m else: value = raw_input(prompt) while(True): if value == "": if default: map[key] = param.prototype break try: map[key] = pclass(value) break except: print "Invalid entry" print map # The last parameter is how long to wait as an RInt proc = scriptService.runScript(scriptId, map, None) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) # handle any results from the script #print results.keys() if 'Message' in results: print "\nRESULTS:", results['Message'].getValue() for result in results.keys(): if result not in ["Message", "stdout", "stderr"]: print "\n", result, results[result].getValue().__class__ printOutErr = True if printOutErr: rawFileService = session.createRawFileStore() queryService = session.getQueryService() if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdOut in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdErr in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId)
def script_results(request, jobId, conn=None, **kwargs): if jobId not in request.session['processors']: return HttpResponse( "Results not found (may have already been returned)") request.session.modified = True # allows us to modify session... procString = request.session['processors'][jobId] del request.session['processors'][ jobId] # delete this, since we cannot use it again to get the results client = conn.c proc = omero.grid.ScriptProcessPrx.checkedCast( client.ic.stringToProxy(procString)) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) message = None # Handle the expected 'Message' in results. if 'Message' in results: message = results['Message'].getValue() # if we have stdout or stderr, download the file and return it. rawFileService = conn.createRawFileStore() queryService = conn.getQueryService() stdout = None stderr = None if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() stdout = scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() stderr = scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) # look for any other string values and images in results... resultMap = {} strings = [] images = [] for key, value in results.items(): if key not in ["Message", "stdout", "stderr"]: obj = value.getValue() # if rstring, value is "string" if type(obj) == type(""): strings.append({"key": key, "value": obj}) elif type(obj) == omero.model.ImageI: images.append({ "key": key, "name": obj.getName().getValue(), "id": obj.getId().getValue() }) elif type(obj) == omero.model.DatasetI: resultMap['dataset'] = { "name": obj.getName().getValue(), "id": obj.getId().getValue() } resultMap['strings'] = strings images.sort(key=lambda i: i["id"]) resultMap['images'] = images # html will give users links to any Image, stdout, stderr and any strings returned in results return render_to_response( 'webemdb/scripts/script_results.html', { 'message': message, 'resultMap': resultMap, 'stdout': stdout, 'stderr': stderr })
def script_results(request, jobId): if jobId not in request.session["processors"]: return HttpResponse("Results not found (may have already been returned)") request.session.modified = True # allows us to modify session... procString = request.session["processors"][jobId] del request.session["processors"][jobId] # delete this, since we cannot use it again to get the results conn = getConnection(request) client = conn.c proc = omero.grid.ScriptProcessPrx.checkedCast(client.ic.stringToProxy(procString)) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) message = None # Handle the expected 'Message' in results. if "Message" in results: message = results["Message"].getValue() # if we have stdout or stderr, download the file and return it. rawFileService = conn.createRawFileStore() queryService = conn.getQueryService() stdout = None stderr = None if "stdout" in results: origFile = results["stdout"].getValue() fileId = origFile.getId().getValue() stdout = scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if "stderr" in results: origFile = results["stderr"].getValue() fileId = origFile.getId().getValue() stderr = scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) # look for any other string values and images in results... resultMap = {} strings = [] images = [] for key, value in results.items(): if key not in ["Message", "stdout", "stderr"]: obj = value.getValue() # if rstring, value is "string" if type(obj) == type(""): strings.append({"key": key, "value": obj}) elif type(obj) == omero.model.ImageI: images.append({"key": key, "name": obj.getName().getValue(), "id": obj.getId().getValue()}) elif type(obj) == omero.model.DatasetI: resultMap["dataset"] = {"name": obj.getName().getValue(), "id": obj.getId().getValue()} resultMap["strings"] = strings images.sort(key=lambda i: i["id"]) resultMap["images"] = images # html will give users links to any Image, stdout, stderr and any strings returned in results return render_to_response( "webemdb/scripts/script_results.html", {"message": message, "resultMap": resultMap, "stdout": stdout, "stderr": stderr}, )
def runScript(session, scriptService, scriptPath): """ This will attempt to run the script (scriptPath can be path or ID), asking the user for inputs for all the script parameters, and printing out the results when the script completes. """ try: scriptId = long(scriptPath) except: scriptId = scriptService.getScriptID(scriptPath) # Identify the script we want to run: Get all 'official' scripts and # filter by path. print "Running script: %s with ID: %s" % (scriptPath, scriptId) # make a map of all the parameters we want to pass to the script # keys are strings. Values must be omero.rtypes such as rlong, rbool, # rlist. map = {} params = scriptService.getParams(scriptId) for key, param in params.inputs.items(): print "" print key if param.description: print param.description if not param.optional: print " * Required" if param.values: print "Options:", ", ".join(unwrap(param.values)) if param.min: print "Min:", param.min.getValue() if param.max: print "Max:", param.max.getValue() prototype = param.prototype prompt = ": " default = None if param.useDefault: default = param.prototype.val prompt = "[%s]: " % default pclass = prototype.__class__ if pclass == omero.rtypes.RListI: valueList = [] listClass = omero.rtypes.rstring l = prototype.val # list # check if a value type has been set (first item of prototype # list) if len(l) > 0: listClass = l[0].getValue().__class__ if listClass == int(1).__class__: listClass = omero.rtypes.rint if listClass == long(1).__class__: listClass = omero.rtypes.rlong print "List:" while (True): value = raw_input(prompt) if value == "": break try: obj = listClass(value) except: print "Invalid entry" continue if isinstance(obj, omero.model.IObject): valueList.append(omero.rtypes.robject(obj)) else: valueList.append(obj) if len(valueList) > 0: map[key] = omero.rtypes.rlist(valueList) elif pclass == omero.rtypes.RMapI: print "MAP!" # check if a value type has been set for the map m = prototype.val print m else: value = raw_input(prompt) while (True): if value == "": if default: map[key] = param.prototype break try: map[key] = pclass(value) break except: print "Invalid entry" print map # The last parameter is how long to wait as an RInt proc = scriptService.runScript(scriptId, map, None) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) # handle any results from the script # print results.keys() if 'Message' in results: print "\nRESULTS:", results['Message'].getValue() for result in results.keys(): if result not in ["Message", "stdout", "stderr"]: print "\n", result, results[result].getValue().__class__ printOutErr = True if printOutErr: rawFileService = session.createRawFileStore() queryService = session.getQueryService() if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() print("\n******** Script generated StdOut in file:%s *******" % fileId) print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() print("\n******** Script generated StdErr in file:%s *******" % fileId) print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) rawFileService.close()
def runScript(session, scriptService, scriptPath): # Identify the script we want to run: Get all 'my' scripts and filter by path. acceptsList = [ ] # An empty list implies that the server should return what it would by default trust. scripts = scriptService.getUserScripts( acceptsList) # returns list of OriginalFiles for s in scripts: print s.id.val, s.path.val + s.name.val namedScripts = [ s.id.val for s in scripts if s.path.val + s.name.val == scriptPath ] if len(namedScripts) == 0: print "Didn't find any scripts with specified path" # use the most recent script (highest ID) scriptId = max(namedScripts) print "Running script: %s with ID: %s" % (scriptPath, scriptId) # Don't attempt to run script without starting user processor! # return # make a map of all the parameters we want to pass to the script # keys are strings. Values must be omero.rtypes such as rlong, rbool, rlist. map = { "Input_Message": omero.rtypes.rstring("Sending this message to the server!"), } # The last parameter is how long to wait as an RInt proc = scriptService.runScript(scriptId, map, None) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) # handle any results from the script #print results.keys() if 'Message' in results: print results['Message'].getValue() rawFileService = session.createRawFileStore() queryService = session.getQueryService() if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdOut in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() print "\n******** Script generated StdErr in file:%s *******" % fileId print scriptUtil.readFromOriginalFile(rawFileService, queryService, fileId)
def run(commandArgs): # login details host = commandArgs["host"] user = commandArgs["username"] password = commandArgs["password"] client = omero.client(host) session = client.createSession(user, password) # create the services we need scriptService = session.getScriptService() rawFileStore = session.createRawFileStore() queryService = session.getQueryService() re = session.createRenderingEngine() updateService = session.getUpdateService() imageId = None if "image" in commandArgs: imageId = long(commandArgs["image"]) else: print "No image ID given" return # get the most recent (highest ID) script with the correct name scriptName = "/EMAN2/Save_Image_As_Em.py" scriptId = scriptService.getScriptID(scriptName) print "Running %s with script ID: %s" % (scriptName, scriptId) imageIds = omero.rtypes.rlist([omero.rtypes.rlong(imageId)]) map = { "Image_IDs": imageIds, "Extension": omero.rtypes.rstring("mrc") # export as mrc file } results = None proc = scriptService.runScript(scriptId, map, None) try: cb = omero.scripts.ProcessCallbackI(client, proc) while not cb.block(1000): # ms. pass cb.close() results = proc.getResults(0) # ms finally: proc.close(False) path = None if "path" in commandArgs: path = commandArgs["path"] fileNames = [] # need names for passing to chimera if "Original_Files" in results: for r in results["Original_Files"].getValue(): # download the file from OMERO f = r.getValue() fileId = f.getId().getValue() print "Downloading Original File ID:", fileId originalFile = queryService.findByQuery("from OriginalFile as o where o.id = %s" % fileId, None) name = originalFile.getName().getValue() if path: name = os.path.join(path, name) filePath = scriptUtil.downloadFile(rawFileStore, originalFile, name) print " file saved to:", filePath fileNames.append(filePath) # if 'name' file already exists, filePath will be different # This only deletes the DB row, not the data on disk! utils.cleanse.py removes files that are not in db. updateService.deleteObject(originalFile) else: print "No OriginalFileIds returned by script" if 'stdout' in results: origFile = results['stdout'].getValue() fileId = origFile.getId().getValue() print "\n******** Script: %s generated StdOut in file:%s *******" % (scriptName, fileId) print scriptUtil.readFromOriginalFile(rawFileStore, queryService, fileId) if 'stderr' in results: origFile = results['stderr'].getValue() fileId = origFile.getId().getValue() print "\n******** Script: %s generated StdErr in file:%s *******" % (scriptName, fileId) print scriptUtil.readFromOriginalFile(rawFileStore, queryService, fileId) return # need to get colours for each channel, to pass to chimera. Chimera uses [(0.0, 0.0, 1.0, 1.0),(0.0, 1.0, 0.0, 1.0)] # This returns e.g. [[0, 0, 255, 255], [0, 255, 0, 255], [255, 0, 0, 255], [255, 0, 0, 255]] but seems to work OK. pixels = containerService.getImages('Image', [imageId], None)[0].getPrimaryPixels() pixelsId = pixels.getId().getValue() sizeC = pixels.getSizeC().getValue() colours = getColours(re, sizeC, pixelsId) # now we need to make a Chimera script to open the images (channels) and colour them! scriptLines = [] scriptLines.append("from chimera import openModels") scriptLines.append("from VolumeViewer import Volume") for fn in fileNames: scriptLines.append("openModels.open('%s')" % fn) scriptLines.append("colors = %s" % colours) # the colours list is rendered suitably as a string scriptLines.append("for c, v in enumerate(openModels.list(modelTypes=[Volume])):") scriptLines.append(" v.set_parameters(surface_colors = [colors[c]])") scriptLines.append(" v.show()") print "\n".join(scriptLines) scriptName = "colourMaps.py" f = open(scriptName, 'w') # will overwrite each time. for line in scriptLines: f.write(line) f.write("\n") f.close() command = "chimera --script %s" % scriptName print command os.system(command)