def run_as_program(): """ Testing function to allow the script to be called outside of the OMERO scripting environment. The connection details and image ID must be valid. """ import getpass HOST = 'localhost' PORT = 4064 USERNAME = raw_input("OMERO username: "******"OMERO password: "******"OMERO host (%s): " % HOST) if h: HOST = h p = raw_input("OMERO port (%d): " % PORT) if p: PORT = p conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() conn.keepAlive() params = create_script_defaults() params[PARAM_DATATYPE] = 'Image' params[PARAM_IDS] = [51] # params[PARAM_DATATYPE] = 'Dataset' # params[PARAM_IDS] = [2] params[PARAM_UPLOAD_RESULTS] = True params[PARAM_EMAIL_RESULTS] = True params[PARAM_EMAIL] = ADMIN_EMAIL count = run(conn, params) if count >= 0: print ("Processed %d image%s" % (count, count != 1 and 's' or ''))
def test_unicode_password(self): with pytest.raises(Ice.ConnectionRefusedException): gateway = BlitzGateway( username='******', passwd=u'ążźćółę', host='localhost', port=65535 ) gateway.connect()
def run_as_program(): """ Testing function to allow the script to be called outside of the OMERO scripting environment. The connection details and image ID must be valid. """ import getpass HOST = 'localhost' PORT = 4064 USERNAME = raw_input("OMERO username: "******"OMERO password: "******"OMERO host (%s): " % HOST) if h: HOST = h p = raw_input("OMERO port (%d): " % PORT) if p: PORT = p conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() params = {} params[PARAM_IDS] = [1] params[PARAM_DATATYPE] = "Image" params[PARAM_ALL_IMAGES] = True params[PARAM_READABLE] = True (count, ok) = run(conn, params) print (summary(count, ok))
def main(argv): parser = make_parser() args = parser.parse_args(argv[1:]) if not args.out_file: args.out_file = "map_screen_%d.tsv" % args.screen_id passwd = getpass.getpass() conn = BlitzGateway( args.user, passwd, host=args.host, port=args.port, group=args.group ) conn.connect() screen = conn.getObject("Screen", args.screen_id) print "writing to %s" % args.out_file print "SCREEN: %s" % screen.name with open(args.out_file, "w") as fo: fo.write("PLATE\tSERIES\tWELL\tFIELD\tImageID\tWellID\n") for p in screen.listChildren(): rows = [] print " plate: %s" % p.name for w in p.listChildren(): n_fields = sum(1 for _ in w.listChildren()) for i in xrange(n_fields): img = w.getImage(i) well_tag = "%s%02d" % (LETTERS[w.row], w.column + 1) rows.append(( p.name, img.getSeries(), well_tag, i + 1, img.id, w.id )) rows.sort(key=itemgetter(1)) rows.sort() for r in rows: fo.write("%s\t%d\t%s\t%d\t%d\t%d\n" % r)
def connect_to_omero(uname, passwd): conn = BlitzGateway(uname, passwd, host=HOST, port=PORT) connected = conn.connect() if not connected: sys.stderr.write("Error : Connection not available, please check your user name and password.\n") return None return conn
def runAsScript(): """ The main entry point of the script, as called by the client via the scripting service, passing the required parameters. """ client = scripts.client('CLI Test.py', "Test script/CLI interactions") try: conn = BlitzGateway(client_obj=client) cli = omero.cli.CLI() cli.loadplugins() cmd = [] cmd.extend(["login"]) cmd.extend(["-s", "localhost"]) # cmd.extend(["-g", conn.getGroupFromContext().getName()]) cmd.extend(["-p", "4064"]) cmd.extend(["-k", conn._getSessionId()]) cli.invoke(cmd, strict=True) finally: client.closeSession()
def processImages(client, scriptParams): message = '' # for params with default values, we can get the value directly dataType = scriptParams['Data_Type'] ids = scriptParams['IDs'] # Get the datasets conn = BlitzGateway(client_obj = client) objects, logMessage = script_utils.getObjects(conn, scriptParams) message += logMessage if not objects: return message datasets = conn.getObjects(dataType, ids) good, chNames, msg = checkChannels(datasets) message += msg if not good: raise omero.ServerError( 'Channel check failed, ' + 'all images must have the same channels: %s' % message) return message
def testSimpleDelete(self): filename = self.unique_dir + "/file.txt" mrepo = self.getManagedRepo() ofile = self.createFile(mrepo, filename) gateway = BlitzGateway(client_obj=self.client) # Assert contents of file rfs = mrepo.fileById(ofile.id.val) try: assert "hi" == rfs.read(0, 2) finally: rfs.close() handle = gateway.deleteObjects("/OriginalFile", [ofile.id.val]) try: gateway._waitOnCmd(handle) finally: handle.close() # Trying to open the file should not throw an UnregisteredFileException # But should just be an empty file. rfs = mrepo.file(filename, "rw") try: assert "\x00\x00" == rfs.read(0, 2) finally: rfs.close()
def testFakeImport(self): # TODO: should likely be in the "fs" namespace req = omero.cmd.OriginalMetadataRequest() client = self.new_client() rsp = self.fullImport(client) # Note: fake test produces no metadata! image = rsp.objects[0] req.imageId = image.id.val gateway = BlitzGateway(client_obj=client) # Load via the gateway image = gateway.getObject("Image", image.id.val) assert 3 == len(image.loadOriginalMetadata()) # Load via raw request handle = client.sf.submit(req) try: gateway._waitOnCmd(handle, failonerror=True) rsp = handle.getResponse() assert dict == type(rsp.globalMetadata) assert dict == type(rsp.seriesMetadata) finally: handle.close()
def testChgrpAsync(self): """ Try to reproduce "race condition" bugs seen in web #8037 (fails to reproduce) """ image = self.image ctx = self.gateway.getAdminService().getEventContext() uuid = ctx.sessionUuid self.loginAsAdmin() gid = self.gateway.createGroup("chgrp-test-%s" % uuid, member_Ids=[ctx.userId], perms=COLLAB) self.loginAsAuthor() original_group = ctx.groupId self.assertNotEqual(None, self.gateway.getObject("Image", image.id)) # Do the Chgrp rsp = self.doChange("Image", image.getId(), gid, return_complete=False) while rsp.getResponse() is None: # while waiting, try various things to reproduce race condition seen in web. img = self.gateway.getObject("Image", image.id) c = BlitzGateway() c.connect(sUuid=uuid) #self.gateway.setGroupForSession(gid) # Image should no-longer be available in current group self.assertEqual(None, self.gateway.getObject("Image", image.id), "Image should not be available in original group") # Switch to new group - confirm that image is there. self.gateway.setGroupForSession(gid) img = self.gateway.getObject("Image", image.id) self.assertNotEqual(None, img, "Image should be available in new group") self.assertEqual(img.getDetails().getGroup().id, gid, "Image group.id should match new group")
def __init__(self, conn=None, user=None, passwd=None, server=SERVER, port=PORT, skey=None): """ Requires active Blitz connection OR username plus password or sesskey """ if conn is None and (user is None or (passwd is None and skey is None)): raise ValueError("Bad parameters," + self.__init__.__doc__) if conn is not None: if conn.isConnected(): self.conn = conn else: raise ValueError("Cannot initialize with closed connection!") else: if passwd is not None: self.conn = BlitzGateway(user, passwd, host=server, port=port) self.conn.connect() else: self.conn = BlitzGateway(user, host=server, port=port) self.conn.connect(skey) if self.conn.isConnected(): self._server = self.conn.host self._port = self.conn.port self._user = self.conn.getUser().getName() self._key = self.conn.getSession().getUuid().getValue() print("Connected to {0} (port {1}) as {2}, session key={3}".format( self._server, self._port, self._user, self._key)) else: print("Failed to open connection :-(")
def check(self): from omero.cli import CLI from omero.gateway import BlitzGateway cli = CLI() cli.loadplugins() cli.onecmd('login -q') try: gateway = BlitzGateway(client_obj=cli.get_client()) for experiment in self.m["experiments"]: self.check_object(gateway, experiment, "Project") for experiment in self.m["screens"]: self.check_object(gateway, experiment, "Screen") if "map" in self.m: if self.m["experiments"]: study_type = "Project" else: study_type = "Screen" self.check_object(gateway, self.m, study_type) finally: if cli: cli.close() gateway.close()
def get_connection(user, group_id=None): """Get a BlitzGateway connection for the given user's client.""" connection = BlitzGateway(client_obj=user[0]) # Refresh the session context connection.getEventContext() if group_id is not None: connection.SERVICE_OPTS.setOmeroGroup(group_id) return connection
def connect_to_omero(): conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) connected = conn.connect() if not connected: import sys sys.stderr.write("Error : Connection not available, please check your user name and password.\n") return None return conn
def test_chgrp_new_container(self, dataset, credentials): """ Performs a chgrp POST, polls the activities json till done, then checks that Dataset has moved to new group and has new Project as parent. """ django_client = self.get_django_client(credentials) request_url = reverse('chgrp') projectName = "chgrp-project%s" % (self.uuid()) data = { "group_id": self.group2.id.val, "Dataset": dataset.id.val, "new_container_name": projectName, "new_container_type": "project", } data = _csrf_post_response_json(django_client, request_url, data) expected = {"update": {"childless": {"project": [], "orphaned": False, "dataset": []}, "remove": {"project": [], "plate": [], "screen": [], "image": [], "dataset": [dataset.id.val]}}} assert data == expected activities_url = reverse('activities_json') data = _get_response_json(django_client, activities_url, {}) # Keep polling activities until no jobs in progress while data['inprogress'] > 0: time.sleep(0.5) data = _get_response_json(django_client, activities_url, {}) # individual activities/jobs are returned as dicts within json data for k, o in data.items(): if hasattr(o, 'values'): # a dict if 'report' in o: print o['report'] assert o['status'] == 'finished' assert o['job_name'] == 'Change group' assert o['to_group_id'] == self.group2.id.val # Dataset should now be in new group, contained in new Project conn = BlitzGateway(client_obj=self.client) userId = conn.getUserId() conn.SERVICE_OPTS.setOmeroGroup('-1') d = conn.getObject("Dataset", dataset.id.val) assert d is not None assert d.getDetails().group.id.val == self.group2.id.val p = d.getParent() assert p is not None assert p.getName() == projectName # Project owner should be current user assert p.getDetails().owner.id.val == userId
def testCmdDeleteCantDeleteDirectories(self): id = self.dir_map[self.dir_key]["id"] gateway = BlitzGateway(client_obj=self.client) handle = gateway.deleteObjects("/OriginalFile", [id]) try: pytest.raises(Exception, gateway._waitOnCmd, handle, failonerror=True) finally: handle.close()
def ls(self, args): """List all the original files contained in a fileset""" client = self.ctx.conn(args) gateway = BlitzGateway(client_obj=client) gateway.SERVICE_OPTS.setOmeroGroup("-1") fileset = gateway.getObject("Fileset", args.fileset.id.val) defaultdict(list) for ofile in fileset.listFiles(): print ofile.path + ofile.name
def connect_base(): """Establish the base connection to OMERO. Returns the connection in the context defined in localconfig. """ # establish the base connection with an admin account su_conn = BlitzGateway(SU_USER, SU_PASS, host=HOST, port=PORT) if su_conn.connect() is False: raise RuntimeError('Connection to OMERO failed, check settings!') log.debug("Successfully connected to OMERO.") return su_conn
def temporary_connection(): temp_client = omero.client() omeroProperties = temp_client.getProperties().getPropertiesForPrefix('omero') # Configuration HOST = omeroProperties.get('omero.host', 'localhost') PORT = omeroProperties.get('omero.port', 4064) USERNAME = omeroProperties.get('omero.user') PASSWORD = omeroProperties.get('omero.pass') temp_conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) connected = temp_conn.connect() return temp_client,temp_conn
def setup_method(self, method): super(MetadataTestBase, self).setup_method(method) self.name = self.uuid() self.image = self.importSingleImage( GlobalMetadata={'gmd-' + self.name: 'gmd-' + self.name}) conn = BlitzGateway(client_obj=self.client) self.imageid = unwrap(self.image.getId()) assert type(self.imageid) == long wrapper = conn.getObject("Image", self.imageid) self.md = Metadata(wrapper)
def chmodGroupAs(self, user, perms): client = self.clients[user] if True: # Note: deprecated client.sf.getAdminService().changePermissions( self.group, omero.model.PermissionsI(perms)) else: from omero.gateway import BlitzGateway from omero.cmd import ERR gateway = BlitzGateway(client_obj=client) handle = gateway.chmodGroup(self.group.id.val, perms) cb = gateway._waitOnCmd(handle) rsp = cb.getResponse() if isinstance(rsp, ERR): raise Exception(rsp)
def connect( server, port, username, password ): ''' Helper method that connects to an OMERO.searcher server. @param server @param port @param username @param password @returns connection ''' try: conn = BlitzGateway( username, password, host=server, port=int(port)) conn.connect() return conn except: return None
def __init__(self, user = None, passwd = None, host = None, client = None): """ Create a new client session, either by specifying user and passwd or by providing a client object (for scripts) @param user Username @param passwd Password @param host The server hostname @param client Client object with an active session """ self.log = logging.getLogger(__name__) #self.log.setLevel(logging.DEBUG) if not client: client = omero.client(host) sess = client.createSession(user, passwd) client.enableKeepAlive(60) else: sess = client.getSession() self.conn = BlitzGateway(client_obj = client) self.res = sess.sharedResources() if (not self.res.areTablesEnabled()): raise TableConnectionError('OMERO.tables not enabled')
def __init__(self, user = None, passwd = None, host = 'localhost', client = None, tableName = None, tableId = None): """ Create a new table handler, either by specifying user and passwd or by providing a client object (for scripts) @param user Username @param passwd Password @param host The server hostname @param client Client object with an active session @param tableName The name of the table file @param tableId The OriginalFile ID of the table file """ if not client: client = omero.client(host) sess = client.createSession(user, passwd) client.enableKeepAlive(60) else: sess = client.getSession() self.conn = BlitzGateway(client_obj = client) self.res = sess.sharedResources() if (not self.res.areTablesEnabled()): raise TableConnectionError('OMERO.tables not enabled') repos = self.res.repositories() self.rid = repos.descriptions[0].id.val self.tableName = tableName self.tableId = tableId self.table = None
def __init__(self, user="******", pwd="omero", host="localhost", port=4064): self.conn = BlitzGateway(user, pwd, host=host, port=port) result = self.conn.connect() if not result: raise RuntimeError("Cannot connect to <{0}>".format(host)+ " with user <{0}>.".format(user)) self.svc = self.conn.getScriptService()
def create_containers(cli, dataset, project=None): """ Creates containers with names provided if they don't exist already. Returns Dataset ID. """ sessionId = cli._event_context.sessionUuid conn = BlitzGateway() conn.connect(sUuid = sessionId) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = wrap(conn.getUser().getId()) d = None prId = None if project is not None: p = conn.getObject("Project", attributes={'name': project}, params=params) if p is None: print "Creating Project:", project p = omero.model.ProjectI() p.name = wrap(project) prId = conn.getUpdateService().saveAndReturnObject(p).id.val else: print "Using Project:", project, p prId = p.getId() # Since Project already exists, check children for Dataset for c in p.listChildren(): if c.getName() == dataset: d = c if d is None: d = conn.getObject("Dataset", attributes={'name': dataset}, params=params) if d is None: print "Creating Dataset:", dataset d = omero.model.DatasetI() d.name = wrap(dataset) dsId = conn.getUpdateService().saveAndReturnObject(d).id.val if prId is not None: print "Linking Project-Dataset..." link = omero.model.ProjectDatasetLinkI() link.child = omero.model.DatasetI(dsId, False) link.parent = omero.model.ProjectI(prId, False) conn.getUpdateService().saveObject(link) else: print "Using Dataset:", dataset, d dsId = d.getId() return dsId
class Connection(object): """ A wrapper for managing a client session context """ def __init__(self, user = None, passwd = None, host = None, client = None): """ Create a new client session, either by specifying user and passwd or by providing a client object (for scripts) @param user Username @param passwd Password @param host The server hostname @param client Client object with an active session """ self.log = logging.getLogger(__name__) #self.log.setLevel(logging.DEBUG) if not client: client = omero.client(host) sess = client.createSession(user, passwd) client.enableKeepAlive(60) else: sess = client.getSession() self.conn = BlitzGateway(client_obj = client) self.res = sess.sharedResources() if (not self.res.areTablesEnabled()): raise TableConnectionError('OMERO.tables not enabled') def __enter__(self): self.log.debug('Entering Connection') return self def __exit__(self, type, value, traceback): self.log.debug('Exiting Connection') self.close() def close(self): """ Child classes can override this method but must explcitly call this method to ensure the client session is cleaned """ self.log.debug('Closing Connection') self.conn._closeSession()
def setup_method(self, method): # prepare session self.r = RequestFactory().get('/rand') middleware = SessionMiddleware() middleware.process_request(self.r) self.r.session.save() self.rs = self.root.sf.getConfigService() self.conn = BlitzGateway(client_obj=self.new_client())
def run_as_script(): """ Main entry point of the script, as called via the scripting service. """ client = scripts.client( 'Export_Tree_Hierarchy.py', 'Trigger an update of the symlink tree hierarchy on the sciCORE ' 'cluster filesystem.', authors = ["Niko Ehrenfeuchter"], institutions = ["IMCF, University of Basel"], contact = "*****@*****.**", ) try: # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) username = conn.getUser().getName() markdir = os.path.join(os.environ['HOME'], '.omero_tree_export_usernames') if not os.path.exists(markdir): # do not create the marker directory, send back an error message # instead - the directory has to exist, otherwise the wrapper # daemon is not running! message = "ERROR: Marker directory '%s' missing!" % markdir client.setOutput("Message", rstring(message)) raise IOError("directory '%s' missing!" % markdir) filename = os.path.join(markdir, username) if os.path.exists(filename): message = ("WARNING: a request for username '%s' is already " "existing! Please contact an administrator if this " "request does not get processed soon!" % username) else: message = "Requested update for username '%s'." % username with open(filename, 'a') as out: out.write('%s' % username) client.setOutput("Message", rstring(message)) finally: # Cleanup client.closeSession()
class ScriptsManager(object): def __init__(self, user="******", pwd="omero", host="localhost", port=4064): self.conn = BlitzGateway(user, pwd, host=host, port=port) result = self.conn.connect() if not result: raise RuntimeError("Cannot connect to <{0}>".format(host)+ " with user <{0}>.".format(user)) self.svc = self.conn.getScriptService() def list(self): return self.svc.getScripts() def delete(self, id): try: self.svc.deleteScript(id) except Exception, e: raise RuntimeError("Failed to delete script: "+ "{0} ({1})".format(id, e))
def clean(image_id, omero_username, omero_password, omero_host, omero_secured, verbose): with BlitzGateway(omero_username, omero_password, host=omero_host, secure=omero_secured) as conn: roi_service = conn.getRoiService() img = conn.getObject('Image', image_id) rois = roi_service.findByImage(image_id, None, conn.SERVICE_OPTS).rois # Delete existing rois if len(rois) > 0: if verbose: print(f"Removing {len(rois)} existing ROIs.") conn.deleteObjects('Roi', [roi.getId().val for roi in rois], wait=True) # Delete existing table named Results_from_Fiji for ann in img.listAnnotations(): if ann.OMERO_TYPE == omero.model.FileAnnotationI: if ann.getFileName() == 'Results_from_Fiji': if verbose: print(f"Removing the table Results_from_Fiji.") conn.deleteObjects('OriginalFile', [ann.getFile().id], wait=True)
def main(instrument_id): loggin = input("OME loggin:") password = getpass("OME password:"******"loggin": loggin, "password": password} manager = Manager() lock = manager.Lock() with BlitzGateway(loggin, password, host=host, port=port) as conn: conn.SERVICE_OPTS.setOmeroGroup("-1") print(instrument_id) #all_images = get_images_from_instrument(instrument_id, conn) all_images = [im.id for im in conn.getObjects("Image")] random.shuffle(all_images) all_images = all_images[:1000] print(f"There are {len(all_images)} images to analyse") pool = Pool(6) results = pool.starmap_async(target, [(lock, im_id, credentials) for im_id in all_images]) results.get()
def run_script(): """ The main entry point of the script, as called by the client via the scripting service, passing the required parameters. """ client = scripts.client( 'Dataset_Images_To_New_Figure.py', """Use Images from a Dataset to replace those in a Figure and save the result as a new Figure, in the same group as the Images""", scripts.String("Data_Type", optional=False, grouping="1", description="Only support Dataset", values=[rstring("Dataset")], default="Dataset"), scripts.List("IDs", optional=False, grouping="2", description="Dataset ID. Only 1 supported").ofType( rlong(0)), scripts.List("Figure_IDs", optional=False, grouping="3", description="Figure ID").ofType(rlong(0)), ) try: conn = BlitzGateway(client_obj=client) params = client.getInputs(unwrap=True) print("params", params) msg = dataset_images_to_new_figure(conn, params) client.setOutput("Message", rstring(msg)) finally: client.closeSession()
def main(args): parser = argparse.ArgumentParser() parser.add_argument('id', help="The id of the project.") parser.add_argument('--username', default="demo") parser.add_argument('--server', default="localhost") parser.add_argument('--port', default=4064) args = parser.parse_args(args) password = getpass.getpass() # Create a connection try: conn = BlitzGateway(args.username, password, host=args.server, port=args.port) conn.connect() load_images(conn, args.id) finally: conn.close()
def connection(): """ Connect to the IDR analysis OMERO server :return: A BlitzGateway object """ # WARNING: The following block is automatically updated by the IDR # deployment scripts. Do not edit this file without testing. # BEGIN ANSIBLE MANAGED BLOCK HOSTNAME = "localhost" USERNAME = "******" PASSWORD = "******" # END ANSIBLE MANAGED BLOCK import omero from omero.gateway import BlitzGateway c = omero.client(HOSTNAME) c.enableKeepAlive(300) c.createSession(USERNAME, PASSWORD) conn = BlitzGateway(client_obj=c) # You are now logged in. In order to guarantee # that the connection is closed if a notebook # does not complete, we are installing a handler # below. This is ONLY for use in a notebook # http://stackoverflow.com/questions/40110540/jupyter-magic-to-handle-notebook-exceptions def custom_exc(shell, etype, evalue, tb, tb_offset=None): print "Closing IDR connection..." c.__del__() shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset) print "Registering handler..." get_ipython().set_custom_exc((Exception, ), custom_exc) return conn
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--verbose', '-v', action='count', default=0, help='Increase the command verbosity') parser.add_argument('--quiet', '-q', action='count', default=0, help='Decrease the command verbosity') parser.add_argument('--dry-run', '-n', action='store_true', help='Run command in dry-run mode') args = parser.parse_args(argv) default_level = logging.INFO - 10 * args.verbose + 10 * args.quiet logging.basicConfig(level=default_level) with cli_login() as c: conn = BlitzGateway(client_obj=c.get_client()) for well in get_corrected_wells(conn): process_well(conn, well, dry_run=args.dry_run)
def run(password, admin_name, target, image, host, port): try: conn = BlitzGateway(admin_name, password, host=host, port=port) conn.connect() conn.SERVICE_OPTS.setOmeroGroup("-1") list_objs(conn, admin_name, target) cut_link(conn, admin_name, image) for i in range(1, 51): username = "******" % i print(username) list_objs(conn, username, target) cut_link(conn, username, image) delete_objs(conn) except Exception as exc: print("Error while cleaning the objects: %s" % str(exc)) finally: conn.close()
def get_mean_zstacks_data(index, imageId, zsize, h, w, Data_F): conn = BlitzGateway('tbacoyannis', 'd33pl34rn1ng', port=4064, host='chinensis.polytechnique.fr') connected = conn.connect() #imageId = 451 image = conn.getObject("Image", imageId) pixels = image.getPrimaryPixels() # get raw pixels information Matrix = np.zeros((zsize, h, w, 3)) x = Data_F.iloc[index].X - w / 2 y = (Data_F.iloc[index].Y) - h / 2 z = (Data_F.iloc[index].Pos) xtiles = np.ones(zsize, dtype=int) * x ytiles = np.ones(zsize, dtype=int) * y tilewidths = np.ones(zsize, dtype=int) * w tileheights = np.ones(zsize, dtype=int) * h tilestacks = zip(xtiles, ytiles, tilewidths, tileheights) zstacks = np.tile(np.arange(z - zsize / 2, z + zsize / 2 + 1), 3) tstacks = np.zeros(zsize, dtype=int) for c in range(0, 3): cstacks = np.ones(zsize, dtype=int) * c liste = zip(zstacks, cstacks, tstacks, tilestacks) pxobj = pixels.getTiles(liste) for i, p in enumerate(pxobj): Matrix[i, :, :, c] = p Norm_Matrix = (Matrix - np.min(Matrix)) * 1.0 / (np.max(Matrix) - np.min(Matrix)) M_mean = np.mean(Norm_Matrix, axis=0) conn._closeSession() return (M_mean)
def run(username, password, host, port): full_names = [ "Francis Crick", "Linda Buck", "Charles Darwin", "Marie Curie", "Alexander Fleming", "Rosalind Franklin", "Robert Hooke", "Jane Goodall", "Gregor Mendel", "Barbara McClintock", "Louis Pasteur", "Ada Lovelace", "Linus Pauling", "Frances Kelsey", "Maurice Wilkins", "Florence Nightingale", "John Sulston", "Elizabeth Blackwell", "Richard Dawkins", "Caroline Dean", "Stephen Reicher", "Wendy Barclay", "Paul Nurse", "Jennifer Doudna", "Adrian Thomas", "Ann Clarke", "Oswald Avery", "Liz Sockett", "Erwin Chargaff", "Tracey Rogers", "Ronald Fisher", "Rachel Carson", "William Harvey", "Nettie Stevens", "Jeffrey Hall", "Youyou Tu", "Michael Rosbash", "Carol Greider", "Yoshinori Ohsumi", "Rosalyn Yalow", "Amedeo Avogadro", "Virginia Apgar", "Kristian Birkeland", "Mary Anning", "Chen-Ning Yang", "Stephanie Kwolek", "Jagadish Bose", "Rita Levi-Montalcini", "Susumu Tonegawa", "Irene Joliot-Curie" ] conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() admin_service = conn.getAdminService() for i, full_name in enumerate(full_names): username = '******' % (i + 1) print(username, full_name) exp = admin_service.lookupExperimenter(username) names = full_name.split(" ") exp.firstName = rstring(names[0]) exp.lastName = rstring(names[1]) admin_service.updateExperimenter(exp) except Exception as exc: print("Error while renaming users: %s" % str(exc)) finally: conn.close()
def main(import_batch_directory, log_directory, timestamp): # Validate import and write import.json conn = BlitzGateway(OMERO_USER, OMERO_PASS, host=OMERO_HOST, port=OMERO_PORT) conn.connect() batch = ImportBatch(conn, import_batch_directory) batch.set_logging(log_directory, timestamp) batch.load_md() if not batch.md: raise ValueError('No metadata file found.') batch.validate_import_md() if not batch.valid_md: raise ValueError('Metadata file has fatal errors.') batch.validate_user_group() batch.set_server_path() batch.load_targets() batch.write_json() conn.close()
def main(args): parser = argparse.ArgumentParser() parser.add_argument('refdir') parser.add_argument('inputdir') parser.add_argument('--username', default="demo") parser.add_argument('--server', default="localhost") parser.add_argument('--port', default=4064) args = parser.parse_args(args) password = getpass.getpass() # Load the image corresponding to the file name # Create a connection try: retrieve_offset(args.refdir) conn = BlitzGateway(args.username, password, host=args.server, port=args.port) conn.connect() conn.c.enableKeepAlive(60) parse_dir(args.inputdir, conn) finally: conn.close() print("done")
def get_or_create_dataset_id(dataset_name, project_name=''): with cli_login(*LOGIN_ARGS) as cli: conn = BlitzGateway(client_obj=cli._client) # handle linking with project if given if project_name != '': # check if the dataset already exists within the project project_id = get_or_create_project_id(conn, project_name) # projects = conn.getContainerService().loadContainerHierarchy('Project', # None, None) project = conn.getObject("Project", project_id) dataset_wrapper = project.findChildByName(dataset_name) if dataset_wrapper != None: return dataset_wrapper.getId() # make a new dataset dataset_id = create_dataset(conn, dataset_name) # link it to the project links = [] link = omero.model.ProjectDatasetLinkI() link.parent = omero.model.ProjectI(project_id, False) link.child = omero.model.DatasetI(dataset_id, False) links.append(link) conn.getUpdateService().saveArray(links, conn.SERVICE_OPTS) return dataset_id # no project specified, check if the dataset already exists datasets = conn.getContainerService().loadContainerHierarchy( 'Dataset', None, None) for d in datasets: if d.getName().getValue() == dataset_name: return d.getId().getValue() # otherwise create a new dataset return create_dataset(conn, dataset_name)
def run_script(): """The main entry point of the script, as called by the client.""" data_types = [ rstring(s) for s in ['Screen', 'Plate', 'Project', 'Dataset', 'Image'] ] client = scripts.client( 'Batch_ROI_Export.py', """Export ROI intensities for selected Images as a CSV file.""", scripts.String("Data_Type", optional=False, grouping="1", description="The data you want to work with.", values=data_types, default="Image"), scripts.List("IDs", optional=False, grouping="2", description="List of Dataset IDs or Image IDs").ofType( rlong(0)), scripts.List( "Channels", grouping="3", default=[1, 2, 3, 4], description="Indices of Channels to measure intensity.").ofType( rint(0)), scripts.Bool("Export_All_Planes", grouping="4", description=("Export all Z and T planes for shapes " "where Z and T are not set?"), default=False), scripts.String("File_Name", grouping="5", default=DEFAULT_FILE_NAME, description="Name of the exported CSV file"), authors=["William Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) log("script_params:") log(script_params) # call the main script result = batch_roi_export(conn, script_params) # Return message and file_annotation to client if result is None: message = "No images found" else: file_ann, message = result if file_ann is not None: client.setOutput("File_Annotation", robject(file_ann._obj)) client.setOutput("Message", rstring(message)) finally: client.closeSession()
# All Rights Reserved. # Use is subject to license terms supplied in LICENSE.txt # """ FOR TRAINING PURPOSES ONLY! """ import omero from omero.rtypes import rint, rlong from omero.gateway import BlitzGateway from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT # Create a connection # ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Configuration # ================================================================= imageId = 67 # The Image we want to Analyse # ================================================================= image = conn.getObject('Image', imageId) # To keep things simple, we'll work with a single Ellipse per T # =================================================================
def download_image_data(image_ids_or_dataset_id, dataset=False, download_original=False, channel=None, z_stack=0, frame=0, coord=(0, 0), width=0, height=0, region_spec='rectangle', skip_failed=False, download_tar=False, omero_host='idr.openmicroscopy.org', omero_secured=False, config_file=None): if config_file is None: # IDR connection omero_username = '******' omero_password = '******' else: # other omero instance with open(config_file) as f: cfg = json.load(f) omero_username = cfg['username'] omero_password = cfg['password'] if omero_username == "" or omero_password == "": omero_username = '******' omero_password = '******' if not download_original and region_spec not in ['rectangle', 'center']: raise ValueError( 'Got unknown value "{0}" as region_spec argument'.format( region_spec)) with ExitStack() as exit_stack: conn = exit_stack.enter_context( BlitzGateway(omero_username, omero_password, host=omero_host, secure=omero_secured)) # exit_stack.callback(conn.connect().close) if download_tar: # create an archive file to write images to archive = exit_stack.enter_context( tarfile.open('images.tar', mode='w')) tempdir = exit_stack.enter_context(TemporaryDirectory()) if dataset: dataset_warning_id = 'Dataset-ID: {0}'.format( image_ids_or_dataset_id[0]) try: dataset_id = int(image_ids_or_dataset_id[0]) except ValueError: image_ids = None else: try: dataset = conn.getObject("Dataset", dataset_id) except Exception as e: # respect skip_failed on unexpected errors if skip_failed: warn(str(e), dataset_warning_id, warn_skip=True) else: raise else: image_ids = [image.id for image in dataset.listChildren()] if image_ids is None: if skip_failed: warn( 'Unable to find a dataset with this ID in the ' 'database.', dataset_warning_id, warn_skip=True) else: raise ValueError( '{0}: Unable to find a dataset with this ID in the ' 'database. Aborting!'.format(dataset_warning_id)) else: # basic argument sanity checks and adjustments prefix = 'image-' # normalize image ids by stripping off prefix if it exists image_ids = [ iid[len(prefix):] if iid[:len(prefix)] == prefix else iid for iid in image_ids_or_dataset_id ] for image_id in image_ids: image_warning_id = 'Image-ID: {0}'.format(image_id) try: image_id = int(image_id) except ValueError: image = None else: try: image = conn.getObject("Image", image_id) except Exception as e: # respect skip_failed on unexpected errors if skip_failed: warn(str(e), image_warning_id, warn_skip=True) continue else: raise if image is None: if skip_failed: warn( 'Unable to find an image with this ID in the ' 'database.', image_warning_id, warn_skip=True) continue raise ValueError( '{0}: Unable to find an image with this ID in the ' 'database. Aborting!'.format(image_warning_id)) if not download_original: try: # try to extract image properties # if anything goes wrong here skip the image # or abort. image_name = os.path.splitext(image.getName())[0] image_warning_id = '{0} (ID: {1})'.format( image_name, image_id) if region_spec == 'rectangle': tile = get_clipping_region(image, *coord, width, height) elif region_spec == 'center': tile = get_clipping_region( image, *_center_to_ul(*coord, width, height)) ori_z, z_stack = z_stack, confine_plane(image, z_stack) ori_frame, frame = frame, confine_frame(image, frame) num_channels = image.getSizeC() if channel is None: channel_index = 0 else: channel_index = find_channel_index(image, channel) except Exception as e: # respect skip_failed on unexpected errors if skip_failed: warn(str(e), image_warning_id, warn_skip=True) continue else: raise # region sanity checks and warnings if tile[2] < width or tile[3] < height: # The downloaded image region will have smaller dimensions # than the specified width x height. warn( 'Downloaded image dimensions ({0} x {1}) will be smaller ' 'than the specified width and height ({2} x {3}).'. format(tile[2], tile[3], width, height), image_warning_id) # z-stack sanity checks and warnings if z_stack != ori_z: warn( 'Specified image plane ({0}) is out of bounds. Using {1} ' 'instead.'.format(ori_z, z_stack), image_warning_id) # frame sanity checks and warnings if frame != ori_frame: warn( 'Specified image frame ({0}) is out of bounds. Using ' 'frame {1} instead.'.format(ori_frame, frame), image_warning_id) # channel index sanity checks and warnings if channel is None: if num_channels > 1: warn( 'No specific channel selected for multi-channel ' 'image. Using first of {0} channels.'.format( num_channels), image_warning_id) else: if channel_index == -1 or channel_index >= num_channels: if skip_failed: warn( str(channel) + ' is not a known channel name for this image.', image_warning_id, warn_skip=True) continue else: raise ValueError( '"{0}" is not a known channel name for image {1}. ' 'Aborting!'.format(channel, image_warning_id)) # download and save the region as TIFF fname = '__'.join([image_name, str(image_id)] + [str(x) for x in tile]) try: if fname[-5:] != '.tiff': fname += '.tiff' fname = fname.replace(' ', '_') im_array = get_image_array(image, tile, z_stack, channel_index, frame) if download_tar: fname = os.path.join(tempdir, fname) try: tiff = TIFF.open(fname, mode='w') tiff.write_image(im_array) finally: tiff.close() # move image into tarball if download_tar: archive.add(fname, os.path.basename(fname)) os.remove(fname) except Exception as e: if skip_failed: # respect skip_failed on unexpected errors warn(str(e), image_warning_id, warn_skip=True) continue else: raise else: try: # try to extract image properties # if anything goes wrong here skip the image # or abort. image_name = os.path.splitext(image.getName())[0] image_warning_id = '{0} (ID: {1})'.format( image_name, image_id) original_image_name = image.getFileset().listFiles( )[0].getName() fname = image_name + "__" + str( image_id) + os.path.splitext(original_image_name)[1] fname = fname.replace(' ', '_') fname = fname.replace('/', '_') download_directory = "./" if download_tar: download_directory = tempdir with cli_login("-u", omero_username, "-s", omero_host, "-w", omero_password) as cli: cli.invoke([ "download", f"Image:{image_id}", download_directory ]) if cli.rv != 0: raise Exception("Download failed.") # This will download to download_directory/original_image_name os.rename( os.path.join(download_directory, original_image_name), os.path.join(download_directory, fname)) # move image into tarball if download_tar: archive.add(os.path.join(download_directory, fname), os.path.basename(fname)) os.remove(os.path.join(download_directory, fname)) except Exception as e: # respect skip_failed on unexpected errors if skip_failed: warn(str(e), image_warning_id, warn_skip=True) continue else: raise
for ds in project.listChildren(): for img in ds.listChildren(): channels = img.getChannels(noRE=True) lc = channels[channel_index].getLogicalChannel() lc.setName(channel_name) lc.save() if len(sys.argv) < 4: printusage() else: channel_index = int(sys.argv[1]) channel_name = sys.argv[2] target = sys.argv[3].split(':') if target[0] == 'Project': project_id = int(target[1]) else: printusage() cli = CLI() cli.loadplugins() cli.onecmd('login -q') try: conn = BlitzGateway(client_obj=cli.get_client()) rename(conn, project_id, channel_index, channel_name) finally: if cli: cli.close() conn.close()
def download_image_data(image_ids, channel=None, z_stack=0, frame=0, coord=(0, 0), width=0, height=0, region_spec='rectangle', skip_failed=False, download_tar=False): # basic argument sanity checks and adjustments prefix = 'image-' # normalize image ids by stripping off prefix if it exists image_ids = [ iid[len(prefix):] if iid[:len(prefix)] == prefix else iid for iid in image_ids ] if region_spec not in ['rectangle', 'center']: raise ValueError( 'Got unknown value "{0}" as region_spec argument'.format( region_spec)) # connect to idr conn = BlitzGateway('public', 'public', host='idr.openmicroscopy.org', secure=True) conn.connect() if download_tar: # create an archive file to write images to archive = tarfile.open('images.tar', mode='w') try: for image_id in image_ids: image_warning_id = 'Image-ID: {0}'.format(image_id) try: image_id = int(image_id) except ValueError: image = None else: try: image = conn.getObject("Image", image_id) except Exception as e: # respect skip_failed on unexpected errors if skip_failed: warn(str(e), image_warning_id, warn_skip=True) continue else: raise if image is None: if skip_failed: warn( 'Unable to find an image with this ID in the ' 'database.', image_warning_id, warn_skip=True) continue raise ValueError( '{0}: Unable to find an image with this ID in the ' 'database. Aborting!'.format(image_warning_id)) try: # try to extract image properties # if anything goes wrong here skip the image # or abort. image_name = os.path.splitext(image.getName())[0] image_warning_id = '{0} (ID: {1})'.format(image_name, image_id) if region_spec == 'rectangle': tile = get_clipping_region(image, *coord, width, height) elif region_spec == 'center': tile = get_clipping_region( image, *_center_to_ul(*coord, width, height)) ori_z, z_stack = z_stack, confine_plane(image, z_stack) ori_frame, frame = frame, confine_frame(image, frame) num_channels = image.getSizeC() if channel is None: channel_index = 0 else: channel_index = find_channel_index(image, channel) except Exception as e: # respect skip_failed on unexpected errors if skip_failed: warn(str(e), image_warning_id, warn_skip=True) continue else: raise # region sanity checks and warnings if tile[2] < width or tile[3] < height: # The downloaded image region will have smaller dimensions # than the specified width x height. warn( 'Downloaded image dimensions ({0} x {1}) will be smaller ' 'than the specified width and height ({2} x {3}).'.format( tile[2], tile[3], width, height), image_warning_id) # z-stack sanity checks and warnings if z_stack != ori_z: warn( 'Specified image plane ({0}) is out of bounds. Using {1} ' 'instead.'.format(ori_z, z_stack), image_warning_id) # frame sanity checks and warnings if frame != ori_frame: warn( 'Specified image frame ({0}) is out of bounds. Using ' 'frame {1} instead.'.format(ori_frame, frame), image_warning_id) # channel index sanity checks and warnings if channel is None: if num_channels > 1: warn( 'No specific channel selected for multi-channel ' 'image. Using first of {0} channels.'.format( num_channels), image_warning_id) else: if channel_index == -1 or channel_index >= num_channels: if skip_failed: warn(str(channel) + ' is not a known channel name for this image.', image_warning_id, warn_skip=True) continue else: raise ValueError( '"{0}" is not a known channel name for image {1}. ' 'Aborting!'.format(channel, image_warning_id)) # download and save the region as TIFF fname = '__'.join([image_name, str(image_id)] + [str(x) for x in tile]) try: if fname[-5:] != '.tiff': fname += '.tiff' fname = fname.replace(' ', '_') im_array = get_image_array(image, tile, z_stack, channel_index, frame) # pack images into tarball if download_tar: tar_img = Image.fromarray(im_array) # Use TemporaryFile() for intermediate storage of images. # TO DO: could this be improved by using # SpooledTemporaryFile with a suitable max_size? with TemporaryFile() as buf: tar_img.save(buf, format='TIFF') tarinfo = tarfile.TarInfo(name=fname) buf.seek(0, 2) tarinfo.size = buf.tell() buf.seek(0) archive.addfile(tarinfo=tarinfo, fileobj=buf) else: # save image as individual file try: tiff = TIFF.open(fname, mode='w') tiff.write_image(im_array) finally: tiff.close() except Exception as e: if skip_failed: # respect skip_failed on unexpected errors warn(str(e), image_warning_id, warn_skip=True) continue else: raise finally: # close the archive file,if it is created if download_tar: archive.close() # Close the connection conn.close()
def runAsScript(): """ The main entry point of the script, as called by the client via the scripting service, passing the required parameters. """ dataTypes = [rstring('Image')] labels = [rstring('Image Name'), rstring('Datasets'), rstring('Tags')] algorithums = [rstring('Maximum Intensity'), rstring('Mean Intensity')] roiLabel = """Specify an ROI to pick by specifying it's shape label. \ 'FigureROI' by default, (not case sensitive). If matching ROI not found, use \ any ROI.""" formats = [rstring('JPEG'), rstring('PNG'), rstring('TIFF')] ckeys = COLOURS.keys() ckeys.sort() oColours = wrap(OVERLAY_COLOURS.keys()) client = scripts.client( 'ROI_Split_Figure.py', """Create a figure of an ROI region as separate zoomed split-channel \ panels. NB: OMERO.insight client provides a nicer UI for this script under \ 'Publishing Options' See http://help.openmicroscopy.org/scripts.html""", # provide 'Data_Type' and 'IDs' parameters so that Insight # auto-populates with currently selected images. scripts.String( "Data_Type", optional=False, grouping="01", description="The data you want to work with.", values=dataTypes, default="Image"), scripts.List( "IDs", optional=False, grouping="02", description="List of Dataset IDs or Image IDs").ofType(rlong(0)), scripts.Map( "Channel_Names", grouping="03", description="Map of index: channel name for All channels"), scripts.Bool( "Merged_Names", grouping="04", description="If true, label the merged panel with channel names." " Otherwise label with 'Merged'"), scripts.List( "Split_Indexes", grouping="05", description="List of the channels in the split view panels"), scripts.Bool( "Split_Panels_Grey", grouping="06", description="If true, all split panels are grayscale"), scripts.Map( "Merged_Colours", grouping="07", description="Map of index:int colors for each merged channel." " Otherwise use existing colour settings"), scripts.Int( "Width", grouping="08", description="Max width of each image panel", min=1), scripts.Int( "Height", grouping="09", description="The max height of each image panel", min=1), scripts.String( "Image_Labels", grouping="10", description="Label images with the Image's Name or it's Datasets" " or Tags", values=labels), scripts.String( "Algorithm", grouping="11", description="Algorithum for projection.", values=algorithums), scripts.Int( "Stepping", grouping="12", description="The Z-plane increment for projection. Default is 1", min=1), scripts.Int( "Scalebar", grouping="13", description="Scale bar size in microns. Only shown if image has" " pixel-size info.", min=1), scripts.String( "Format", grouping="14", description="Format to save image. E.g 'PNG'.", values=formats, default='JPEG'), scripts.String( "Figure_Name", grouping="15", description="File name of the figure to save."), scripts.String( "Overlay_Colour", grouping="16", description="The color of the scale bar.", default='White', values=oColours), scripts.Float( "ROI_Zoom", grouping="17", description="How much to zoom the ROI. E.g. x 2. If 0 then zoom" " roi panel to fit", min=0), scripts.String("ROI_Label", grouping="18", description=roiLabel), version="4.3.0", authors=["William Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: conn = BlitzGateway(client_obj=client) commandArgs = client.getInputs(unwrap=True) print commandArgs # call the main script, attaching resulting figure to Image. Returns # the id of the originalFileLink child. (ID object, not value) fileAnnotation, message = roiFigure(conn, commandArgs) # Return message and file annotation (if applicable) to the client client.setOutput("Message", rstring(message)) if fileAnnotation is not None: client.setOutput("File_Annotation", robject(fileAnnotation._obj)) finally: client.closeSession()
def run_script(): """ The main entry point of the script. Gets the parameters from the scripting service, makes the figure and returns the output to the client. """ data_types = [rstring('Image')] labels = [rstring('Image Name'), rstring('Datasets'), rstring('Tags')] algorithms = [rstring('Maximum Intensity'), rstring('Mean Intensity')] tunits = [ rstring("SECS"), rstring("MINS"), rstring("HOURS"), rstring("MINS SECS"), rstring("HOURS MINS") ] formats = [rstring('JPEG'), rstring('PNG'), rstring('TIFF')] ckeys = COLOURS.keys() ckeys.sort() o_colours = wrap(OVERLAY_COLOURS.keys()) client = scripts.client( 'Movie_Figure.py', """Export a figure of a movie, showing a row of frames for each \ chosen image. NB: OMERO.insight client provides a nicer UI for this script under \ 'Publishing Options' See http://help.openmicroscopy.org/publish.html#movies""", # provide 'Data_Type' and 'IDs' parameters so that Insight # auto-populates with currently selected images. scripts.String("Data_Type", optional=False, grouping="01", description="The data you want to work with.", values=data_types, default="Image"), scripts.List("IDs", optional=False, grouping="02", description="List of Image IDs").ofType(rlong(0)), scripts.List( "T_Indexes", grouping="03", description="The time frames to display in the figure for each" " image").ofType(rint(0)), scripts.String( "Image_Labels", grouping="04", description="Label images with Image name (default) or datasets" " or tags", values=labels), scripts.Int( "Width", grouping="06", description="The max width of each image panel. Default is first" " image width", min=1), scripts.Int( "Height", grouping="07", description="The max height of each image panel. Default is first" " image height", min=1), scripts.Bool("Z_Projection", grouping="08", default=True), scripts.Int( "Z_Start", grouping="08.1", description="Projection range (if not specified, use defaultZ" " only - no projection)", min=0), scripts.Int( "Z_End", grouping="08.2", description="Projection range (if not specified or, use defaultZ" " only - no projection)", min=0), scripts.String("Algorithm", grouping="08.3", description="Algorithm for projection.", values=algorithms), scripts.Int("Stepping", grouping="08.4", description="The Z increment for projection.", default=1, min=1), scripts.Bool("Show_Scalebar", grouping="10", default=True), scripts.Int( "Scalebar", grouping="10.1", description="Scale bar size in microns. Only shown if image has" " pixel-size info.", min=1), scripts.String("Scalebar_Colour", grouping="10.2", description="The color of the scale bar.", default='White', values=o_colours), scripts.String("Format", grouping="11", description="Format to save image.", values=formats, default='JPEG'), scripts.String("Figure_Name", grouping="12", description="File name of the figure to save."), scripts.String("Time_Units", grouping="13", description="The units to use for time display", values=tunits), scripts.Int( "Max_Columns", grouping="04.1", default=10, description="The maximum number of columns in the figure, for" " movie frames.", min=1), version="4.3.0", authors=["William Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: conn = BlitzGateway(client_obj=client) command_args = client.getInputs(unwrap=True) # Makes the figure and attaches it to Image. Returns the id of the # originalFileLink child. (ID object, not value) file_annotation, message = movie_figure(conn, command_args) # Return message and file annotation (if applicable) to the client client.setOutput("Message", rstring(message)) if file_annotation: client.setOutput("File_Annotation", robject(file_annotation._obj)) finally: client.closeSession()
def run(username, password, plate_id, host, port): conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() query_service = conn.getQueryService() # Create a name for the Original File tablename = "Channels_Min_Max_Intensity" # Go through all wells in Plate, adding row for each plate = conn.getObject("Plate", plate_id) wellIds = [] rowData = [] chCount = 0 for well in plate._listChildren(): well = omero.gateway.WellWrapper(conn, well) image = well.getImage() if image is None: continue wellIds.append(well.id) chCount = image.getSizeC() row = [] print("well, image", well.id, image.id) params = omero.sys.ParametersI() params.addId(image.getPixelsId()) query = """select pixels from Pixels as pixels left outer join fetch pixels.channels as channels join fetch channels.statsInfo where pixels.id=:id""" result = query_service.findAllByQuery(query, params) row = [] for pix in result: for ch in pix.iterateChannels(): si = ch.statsInfo row.extend([si.globalMin.val, si.globalMax.val]) rowData.append(row) print('wellIds', wellIds) print('rowData', rowData) # Now we know how many channels, we can make the table col1 = omero.grid.WellColumn('Well', '', []) columns = [col1] colNames = [] for chIdx in range(chCount): for name in ['Ch%sMin' % chIdx, 'Ch%sMax' % chIdx]: colNames.append(name) columns.append(omero.grid.LongColumn(name, '', [])) table = conn.c.sf.sharedResources().newTable(1, tablename) table.initialize(columns) # Add Data from above data1 = omero.grid.WellColumn('Well', '', wellIds) data = [data1] for colIdx in range(len(rowData[0])): colData = [r[colIdx] for r in rowData] print("colData", len(colData)) name = colNames[colIdx] data.append(omero.grid.LongColumn(name, '', colData)) print("Adding data: ", len(data)) table.addData(data) table.close() print("table closed...") orig_file = table.getOriginalFile() fileAnn = omero.model.FileAnnotationI() fileAnn.ns = rstring(NAMESPACE) fileAnn.setFile(omero.model.OriginalFileI(orig_file.id.val, False)) fileAnn = conn.getUpdateService().saveAndReturnObject(fileAnn) link = omero.model.PlateAnnotationLinkI() link.setParent(omero.model.PlateI(plate_id, False)) link.setChild(omero.model.FileAnnotationI(fileAnn.id.val, False)) print("save link...") conn.getUpdateService().saveAndReturnObject(link) except Exception as exc: print("Error while changing names: %s" % str(exc)) finally: conn.close()
scripts.List("IDs", optional=False, grouping="2", description="Dataset or Image IDs.").ofType(rlong(0)), authors=["Will Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: # process the list of args above. scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) print(scriptParams) # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) # Call the main script - returns the new OMERO.figure ann ID figure_id = run(conn, scriptParams) if figure_id is None: message = "No images found" else: message = "Created FRAP figure: %s" % figure_id client.setOutput("Message", rstring(message)) finally: client.closeSession()
def test_chgrp_old_container(self, dataset, credentials): """ Tests Admin moving user's Dataset to their Private group and linking it to an existing Project there. Bug from https://github.com/openmicroscopy/openmicroscopy/pull/3420 """ django_client = self.get_django_client(credentials) # user creates project in their target group project = ProjectI() projectName = "chgrp-target-%s" % self.client.getSessionId() project.name = rstring(projectName) ctx = {"omero.group": native_str(self.group2.id.val)} project = self.sf.getUpdateService().saveAndReturnObject(project, ctx) request_url = reverse('chgrp') data = { "group_id": self.group2.id.val, "Dataset": dataset.id.val, "target_id": "project-%s" % project.id.val, } rsp = post(django_client, request_url, data) data = json.loads(rsp.content) expected = { "update": { "childless": { "project": [], "orphaned": False, "dataset": [] }, "remove": { "project": [], "plate": [], "screen": [], "image": [], "dataset": [dataset.id.val] } } } assert data == expected activities_url = reverse('activities_json') data = get_json(django_client, activities_url) # Keep polling activities until no jobs in progress while data['inprogress'] > 0: time.sleep(0.5) data = get_json(django_client, activities_url) # individual activities/jobs are returned as dicts within json data for k, o in list(data.items()): if hasattr(o, 'values'): # a dict if 'report' in o: print(o['report']) assert o['status'] == 'finished' assert o['job_name'] == 'Change group' assert o['to_group_id'] == self.group2.id.val # Dataset should now be in new group, contained in Project conn = BlitzGateway(client_obj=self.client) userId = conn.getUserId() conn.SERVICE_OPTS.setOmeroGroup('-1') d = conn.getObject("Dataset", dataset.id.val) assert d is not None assert d.getDetails().group.id.val == self.group2.id.val p = d.getParent() assert p is not None assert p.getName() == projectName # Project owner should be current user assert p.getDetails().owner.id.val == userId assert p.getId() == project.id.val
# """ FOR TRAINING PURPOSES ONLY! """ from numpy import zeros, uint8 from omero.gateway import BlitzGateway from Connect_To_OMERO import USERNAME, PASSWORD, HOST, PORT try: from PIL import Image except ImportError: import Image # Create a connection # ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Configuration # ================================================================= imageId = 401 # Save a plane (raw data) as tiff for analysis # ================================================================= image = conn.getObject("Image", imageId) # first plane of the image pixels = image.getPrimaryPixels() # make a note of min max pixel values for each channel # so that we can scale all the planes from each channel to the same range channelMinMax = [] for c in image.getChannels(): minC = c.getWindowMin()
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() datasets = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(datasets) == 0: print("No datasets with name %s found" % target) continue dataset_id = datasets[0].getId().getValue() print('dataset', dataset_id) params2 = omero.sys.ParametersI() params2.addId(dataset_id) query = "select l.child.id from DatasetImageLink l \ where l.parent.id = :id" images = query_service.projection(query, params2, conn.SERVICE_OPTS) for k in range(0, len(images)): image_id = images[k][0].getValue() delta_t = 300 image = conn.getObject("Image", image_id) params = omero.sys.ParametersI() params.addLong('pid', image.getPixelsId()) query = "from PlaneInfo as Info where \ Info.theZ=0 and Info.theC=0 and pixels.id=:pid" info_list = query_service.findAllByQuery( query, params, conn.SERVICE_OPTS) print('info_list', len(info_list)) if len(info_list) == 0: print("Creating info...", image.getSizeT()) info_list = [] for t_index in range(image.getSizeT()): print(' t', t_index) info = PlaneInfoI() info.theT = rint(t_index) info.theZ = rint(0) info.theC = rint(0) info.pixels = PixelsI(image.getPixelsId(), False) dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) info_list.append(info) else: for info in info_list: unwrap_t = unwrap(info.theT) unwrap_z = unwrap(info.theZ) print('theT %s, theZ %s' % (unwrap_t, unwrap_z)) t_index = info.theT.getValue() dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) print("Saving info_list", len(info_list)) conn.getUpdateService().saveArray(info_list) except Exception as exc: print("Error when setting the timestamps: %s" % str(exc)) finally: conn.close()
def test_chgrp_new_container(self, dataset, credentials): """ Performs a chgrp POST, polls the activities json till done, then checks that Dataset has moved to new group and has new Project as parent. """ django_client = self.get_django_client(credentials) request_url = reverse('chgrp') projectName = "chgrp-project%s" % (self.uuid()) data = { "group_id": self.group2.id.val, "Dataset": dataset.id.val, "new_container_name": projectName, "new_container_type": "project", } rsp = post(django_client, request_url, data) data = json.loads(rsp.content) expected = { "update": { "childless": { "project": [], "orphaned": False, "dataset": [] }, "remove": { "project": [], "plate": [], "screen": [], "image": [], "dataset": [dataset.id.val] } } } assert data == expected activities_url = reverse('activities_json') data = get_json(django_client, activities_url) # Keep polling activities until no jobs in progress while data['inprogress'] > 0: time.sleep(0.5) data = get_json(django_client, activities_url) # individual activities/jobs are returned as dicts within json data for k, o in list(data.items()): if hasattr(o, 'values'): # a dict if 'report' in o: print(o['report']) assert o['status'] == 'finished' assert o['job_name'] == 'Change group' assert o['to_group_id'] == self.group2.id.val # Dataset should now be in new group, contained in new Project conn = BlitzGateway(client_obj=self.client) userId = conn.getUserId() conn.SERVICE_OPTS.setOmeroGroup('-1') d = conn.getObject("Dataset", dataset.id.val) assert d is not None assert d.getDetails().group.id.val == self.group2.id.val p = d.getParent() assert p is not None assert p.getName() == projectName # Project owner should be current user assert p.getDetails().owner.id.val == userId
def run_script(): """ The main entry point of the script, as called by the client. Called via the scripting service, passing the required parameters. """ data_types = [rstring('Image')] client = scripts.client( 'Kymograph.py', """This script processes Images, which have Line or PolyLine ROIs to \ create kymographs. Kymographs are created in the form of new OMERO Images, with single Z and T, \ same sizeC as input.""", scripts.String( "Data_Type", optional=False, grouping="1", description="Choose source of images (only Image supported)", values=data_types, default="Image"), scripts.List("IDs", optional=False, grouping="2", description="List of Image IDs to process").ofType( rlong(0)), scripts.Int("Line_Width", optional=False, grouping="3", default=4, description="Width in pixels of each time slice", min=1), scripts.Bool( "Use_All_Timepoints", grouping="4", default=True, description="Use every timepoint in the kymograph. If False, only" " use timepoints with ROI-shapes"), scripts.Float( "Time_Increment", grouping="5", description="If source movie has no time info, specify increment" " per time point (seconds)"), scripts.Float( "Pixel_Size", grouping="6", description="If source movie has no Pixel size info, specify" " pixel size (microns)"), version="4.3.3", authors=["William Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: script_params = client.getInputs(unwrap=True) # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) new_images, message = process_images(conn, script_params) if new_images: if len(new_images) == 1: client.setOutput("New_Image", robject(new_images[0]._obj)) elif len(new_images) > 1: # return the first one client.setOutput("First_Image", robject(new_images[0]._obj)) client.setOutput("Message", rstring(message)) finally: client.closeSession()
def connect(hostname, username, password): conn = BlitzGateway(username, password, host=hostname, secure=True) conn.connect() return conn
description= "The new description to set for each Image in the Dataset"), ) try: # process the list of args above. Not scrictly necessary, but useful for more complex scripts scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput( key, unwrap=True) # unwrap rtypes to String, Integer etc print scriptParams # handy to have inputs in the std-out log # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) # do the editing... editedImgIds = editDescriptions(conn, scriptParams) # now handle the result, displaying message and returning image if appropriate if editedImgIds is None: message = "Script failed. See 'error' or 'info' for more details" else: if len(editedImgIds) == 1: img = conn.getObject("Image", editedImgIds[0]) # image-wrapper message = "One Image edited: %s" % img.getName() omeroImage = img._obj # omero.model object client.setOutput("Edited Image", robject( omeroImage)) # Insight will display 'View' link to image elif len(editedImgIds) > 1:
def run_as_script(): """ The main entry point of the script, as called by the client via the scripting service, passing the required parameters. """ dataTypes = [rstring('Image')] model = [rstring('Exponential') ] #,rstring('Gaussian+Exponential',rstring('Cosine+Exponential')] pc_corr = [rstring('auto'), rstring('cross')] client = scripts.client( 'Pair_Correlation_Function.py', """This script calculates the pair (auto or cross) correlation function for ROIs on PALM/STORM images. This script should only be run on super resolved images where the physical pixel size has been set (e.g. CZI or OME-TIFF files). This script uses code, translated to Python, that was provided in: "Correlation Functions Quantify Super-Resolution Images and Estimate Apparent Clustering Due to Over-Counting" Veatch et al, PlosONE, DOI: 10.1371/journal.pone.0031457 This paper should be referenced in any publication that results from the use of this script.""", scripts.String( "Data_Type", optional=False, grouping="01", description="Choose source of images (only Image supported)", values=dataTypes, default="Image"), scripts.List("IDs", optional=False, grouping="02", description="ID of super resolved image to process"), scripts.String( "Pair_correlation", optional=False, grouping="03", description="Choose the type of pair correlation to perform", values=pc_corr, default="auto"), scripts.Int( "Max_radius", optional=False, grouping="04", description="Maximum distance scale for calculation (in pixels)", default=50), scripts.Bool("Fit_exponential_model", grouping="05", default=True, description="Choose model to fit to correlation data"), scripts.Int("Exponential_amplitude", optional=False, grouping="05.1", description="Amplitude of exponential", default=30), scripts.Int("Exponential_decay", optional=False, grouping="05.2", description="Decay length of exponential (in nm)", default=80), scripts.Int("Exponential_baseline", optional=False, grouping="05.3", description="Baseline of exponential", default=1), scripts.Bool("Fit_exponential+gaussian_model", grouping="06", default=False, description="Choose model to fit to correlation data"), scripts.Int("Density", optional=False, grouping="06.1", description="Surface density of probe (1/um^2)", default=1), scripts.Int("PSF", optional=False, grouping="06.2", description="sqrt(2)*PSF of the image (nm)", default=30), scripts.Int("Amplitude", optional=False, grouping="06.3", description="Amplitude of exponential", default=30), scripts.Int("Decay", optional=False, grouping="06.4", description="Decay length of exponential (in nm)", default=80), scripts.Int("Baseline", optional=False, grouping="06.5", description="Baseline of exponential", default=1), scripts.Bool("Email_Results", grouping="07", default=False, description="E-mail the results"), scripts.String("Email_address", grouping="07.1", description="Specify e-mail address"), authors=["Daniel Matthews", "QBI"], institutions=["University of Queensland"], contact="*****@*****.**", ) try: # process the list of args above. scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) print scriptParams # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) if scriptParams['Email_Results'] and not validate_email( conn, scriptParams): client.setOutput("Message", rstring("No valid email address")) return # process images in Datasets message = run_processing(conn, scriptParams) client.setOutput("Message", rstring(message)) #client.setOutput("Message", rstring("No plates created. See 'Error' or 'Info' for details")) finally: client.closeSession()
def run_script(): """ The main entry point of the script, as called by the client via the scripting service, passing the required parameters. """ data_types = [rstring('Image')] labels = [rstring('Image Name'), rstring('Datasets'), rstring('Tags')] algorithms = [rstring('Maximum Intensity'), rstring('Mean Intensity')] formats = [rstring('JPEG'), rstring('PNG'), rstring('TIFF')] ckeys = COLOURS.keys() ckeys.sort() o_colours = wrap(OVERLAY_COLOURS.keys()) client = scripts.client( 'Split_View_Figure.py', """Create a figure of split-view images. See http://help.openmicroscopy.org/publish.html#figures""", # provide 'Data_Type' and 'IDs' parameters so that Insight # auto-populates with currently selected images. scripts.String("Data_Type", optional=False, grouping="01", description="The data you want to work with.", values=data_types, default="Image"), scripts.List("IDs", optional=False, grouping="02", description="List of Image IDs").ofType(rlong(0)), scripts.String( "Algorithm", grouping="3", description="Algorithm for projection. Only used if a Z-range is" " chosen below", values=algorithms, default='Maximum Intensity'), scripts.Int( "Z_Start", grouping="3.1", description="Projection range (if not specified, use defaultZ" " only - no projection)", min=0), scripts.Int( "Z_End", grouping="3.2", description="Projection range (if not specified, use defaultZ" " only - no projection)", min=0), scripts.Map("Channel_Names", grouping="4", description="Map of index: channel name for all channels"), scripts.List("Split_Indexes", grouping="5", description="List of the channels in the split" " view").ofType(rint(0)), scripts.Bool("Split_Panels_Grey", grouping="6", description="If true, all split panels are grayscale", default=False), scripts.Map( "Merged_Colours", grouping="7", description="Map of index:int colors for each merged channel"), scripts.Bool( "Merged_Names", grouping="8", description="If true, label the merged panel with channel names." " Otherwise label with 'Merged'", default=True), scripts.Int("Width", grouping="9", description="The max width of each image panel. Default is" " first image width", min=1), scripts.Int( "Height", grouping="91", description="The max height of each image panel. Default is" " first image height", min=1), scripts.String( "Image_Labels", grouping="92", description="Label images with Image name (default) or datasets" " or tags", values=labels, default='Image Name'), scripts.Int("Stepping", grouping="93", description="The Z increment for projection.", default=1, min=1), scripts.Int( "Scalebar", grouping="94", description="Scale bar size in microns. Only shown if image has" " pixel-size info.", min=1), scripts.String("Format", grouping="95", description="Format to save image", values=formats, default='JPEG'), scripts.String("Figure_Name", grouping="96", description="File name of the figure to save.", default='Split_View_Figure'), scripts.String("Overlay_Colour", grouping="97", description="The color of the scale bar.", default='White', values=o_colours), version="4.3.0", authors=["William Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) # call the main script, attaching resulting figure to Image. Returns # the FileAnnotationI [file_annotation, message] = split_view_figure(conn, script_params) # Return message and file annotation (if applicable) to the client client.setOutput("Message", rstring(message)) if file_annotation is not None: client.setOutput("File_Annotation", robject(file_annotation._obj)) finally: client.closeSession()