Esempio n. 1
0
 def edit(self, args, config, edit_path=edit_path):
     from omero.util.temp_files import create_path, remove_path
     start_text = "# Edit your preferences below. Comments are ignored\n"
     for k in sorted(config.keys()):
         start_text += ("%s=%s\n" % (k, config[k]))
     temp_file = create_path()
     try:
         edit_path(temp_file, start_text)
     except RuntimeError as re:
         self.ctx.dbg(traceback.format_exc())
         self.ctx.die(
             954, "%s: Failed to edit %s" %
             (getattr(re, "pid", "Unknown"), temp_file))
     args.NAME = config.default()
     old_config = dict(config)
     self.drop(args, config)
     args.file = [open(str(temp_file), "r")]
     args.q = True
     try:
         self.load(args, config)
     except Exception as e:
         for key, value in list(old_config.items()):
             config[key] = value
         raise e
     finally:
         remove_path(temp_file)
Esempio n. 2
0
 def cleanup_tmpdir(self):
     """
     Remove all known files and finally the temporary directory.
     If other files exist, an exception will be raised.
     """
     try:
         remove_path(self.dir)
     except:
         self.logger.error("Failed to remove dir %s" % self.dir, exc_info = True)
Esempio n. 3
0
 def cleanup_tmpdir(self):
     """
     Remove all known files and finally the temporary directory.
     If other files exist, an exception will be raised.
     """
     try:
         remove_path(self.dir)
     except:
         self.logger.error("Failed to remove dir %s" % self.dir,
                           exc_info=True)
Esempio n. 4
0
def as_dictionary(path, readers="", extra_args=None):
    """Run as_stdout, parses the output and returns a dictionary of the form::

    {
    some_file_in_group :
    [
    some_file_in_group
    some_other_file_in_group
    ...
    last_file_in_group
    ],
    some_file_in_second_group : ...
    }

    you can pass more arguments to the `import` command through the
    extra_args argument in the form of a list.

    to_import = as_dictionary("/my/dir/with_tifs", extra_args=["--depth", "6"])

    will go through the directories with a depth level of 6 instead of
    the default 4.  Arguments of interest might be: `debugging`,
    `report`, `logback`. Note that the command runs locally so options
    such as `--exclude` will not work as they need information from the server.
    """
    t = create_path("candidates", "err")

    path = _to_list(path)
    path.insert(0, "---file=%s" % t)
    try:
        as_stdout(path, readers=readers, extra_args=extra_args)
        f = open(str(t), "r")
        output = f.readlines()
        f.close()
    finally:
        remove_path(t)

    gline = -1
    key = None
    groups = {}
    for line in output:
        line = line.strip()
        if len(line) == 0:
            continue
        if line.startswith("#"):
            gline = -1
        else:
            if gline == -1:
                gline = 1
                key = line
                groups[key] = [line]
            else:
                groups[key].append(line)

    return groups
Esempio n. 5
0
    def _checkstd(self, output, which):
        rfile = output.val[which]
        ofile = rfile.val
        assert ofile

        tmppath = create_path("pingtest")
        try:
            self.client.download(ofile, str(tmppath))
            assert os.path.getsize(str(tmppath))
            return tmppath.text()
        finally:
            remove_path(tmppath)
Esempio n. 6
0
    def _checkstd(self, output, which):
        rfile = output.val[which]
        ofile = rfile.val
        assert ofile

        tmppath = create_path("clitest")
        try:
            self.client.download(ofile, str(tmppath))
            assert os.path.getsize(str(tmppath))
            return tmppath.text()
        finally:
            remove_path(tmppath)
def as_dictionary(path, readers=""):
    """
    Run as_stdout, parses the output and returns a dictionary of the form::

        {
            some_file_in_group :
                [
                    some_file_in_group
                    some_other_file_in_group
                    ...
                    last_file_in_group
                ],
            some_file_in_second_group : ...
        }
    """

    t = create_path("candidates", "err")

    path = _to_list(path)
    path.insert(0, "---file=%s" % t)
    try:
        as_stdout(path, readers=readers)
        f = open(str(t), "r")
        output = f.readlines()
        f.close()
    finally:
        remove_path(t)

    gline = -1
    key = None
    groups = {}
    for line in output:
        line = line.strip()
        if len(line) == 0:
            continue
        if line.startswith("#"):
            gline = -1
        else:
            if gline == -1:
                gline = 1
                key = line
                groups[key] = [line]
            else:
                groups[key].append(line)

    return groups
Esempio n. 8
0
def as_dictionary(path, readers=""):
    """
    Run as_stdout, parses the output and returns a dictionary of the form::

        {
            some_file_in_group :
                [
                    some_file_in_group
                    some_other_file_in_group
                    ...
                    last_file_in_group
                ],
            some_file_in_second_group : ...
        }
    """

    t = create_path("candidates", "err")

    path = _to_list(path)
    path.insert(0, "---file=%s" % t)
    try:
        as_stdout(path, readers=readers)
        f = open(str(t), "r")
        output = f.readlines()
        f.close()
    finally:
        remove_path(t)

    gline = -1
    key = None
    groups = {}
    for line in output:
        line = line.strip()
        if len(line) == 0:
            continue
        if line.startswith("#"):
            gline = -1
        else:
            if gline == -1:
                gline = 1
                key = line
                groups[key] = [line]
            else:
                groups[key].append(line)

    return groups
Esempio n. 9
0
 def testBasicUsagePassString(self):
     p = t_f.create_path("foo", ".bar")
     self.assertTrue(p.exists())
     t_f.remove_path(str(p))
     self.assertFalse(p.exists())
 def testBasicUsagePassString(self):
     p = t_f.create_path("foo",".bar")
     self.assertTrue(p.exists())
     t_f.remove_path(str(p))
     self.assertFalse(p.exists())
    def importFile(self, fileName, exName):
        """
            Import file or directory using 'bin/omero importer'
            This method is solely responsible for logging the user in,
            attempting (possibly multiply) an import, logging and
            throwing an exception if necessary.
        """

        key = self.loginUser(exName)
        if not key:
            self.log.info("File not imported: %s", fileName)
            return

        try:
            self.state.appropriateWait(self.throttleImport) # See ticket:5739
            self.log.info("Importing %s (session=%s)", fileName, key)

            imageId = []

            t = create_path("dropbox", "err")
            to = create_path("dropbox", "out")

            cli = omero.cli.CLI()
            cli.loadplugins()
            cmd = ["-s", self.host, "-p", str(self.port), "-k", key, "import"]
            cmd.extend([str("---errs=%s"%t), str("---file=%s"%to), "--", "--agent=dropbox"])
            cmd.extend(shlex.split(self.importArgs))
            cmd.append(fileName)
            logging.debug("cli.invoke(%s)" % cmd)
            cli.invoke(cmd)
            retCode = cli.rv

            if retCode == 0:
                self.log.info("Import of %s completed (session=%s)", fileName, key)
                if to.exists():
                    f = open(str(to),"r")
                    lines = f.readlines()
                    f.close()
                    if len(lines) > 0:
                        for line in lines:
                            imageId.append(line.strip())
                    else:
                        self.log.error("No lines in output file. No image ID.")
                else:
                    self.log.error("%s not found !" % to)

            else:
                self.log.error("Import of %s failed=%s (session=%s)", fileName, str(retCode), key)
                self.log.error("***** start of output from importer-cli to stderr *****")
                if t.exists():
                    f = open(str(t),"r")
                    lines = f.readlines()
                    f.close()
                    for line in lines:
                        self.log.error(line.strip())
                else:
                    self.log.error("%s not found !" % t)
                self.log.error("***** end of output from importer-cli *****")
        finally:
            remove_path(t)
            remove_path(to)

        return imageId
Esempio n. 12
0
            self.ctx.dbg(traceback.format_exc())
            self.ctx.die(954, "%s: Failed to edit %s"
                         % (getattr(re, "pid", "Unknown"), temp_file))
        args.NAME = config.default()
        old_config = dict(config)
        self.drop(args, config)
        args.file = [open(str(temp_file), "r")]
        args.q = True
        try:
            self.load(args, config)
        except Exception as e:
            for key, value in old_config.items():
                config[key] = value
            raise e
        finally:
            remove_path(temp_file)

    @with_config
    def version(self, args, config):
        self.ctx.out(config.version(config.default()))

    @with_config
    def path(self, args, config):
        self.ctx.out(config.filename)

    @with_rw_config
    def lock(self, args, config):
        self.ctx.input("Press enter to unlock")

    @with_rw_config
    def upgrade(self, args, config):
Esempio n. 13
0
        start_text = "# Edit your preferences below. Comments are ignored\n"
        for k in sorted(config.keys()):
            start_text += ("%s=%s\n" % (k, config[k]))
        temp_file = create_path()
        try:
            edit_path(temp_file, start_text)
        except RuntimeError, re:
            self.ctx.dbg(traceback.format_exc())
            self.ctx.die(954, "%s: Failed to edit %s"
                         % (getattr(re, "pid", "Unknown"), temp_file))
        args.NAME = config.default()
        self.drop(args, config)
        args.file = [open(str(temp_file), "r")]
        args.q = True
        self.load(args, config)
        remove_path(temp_file)

    @with_config
    def version(self, args, config):
        self.ctx.out(config.version(config.default()))

    @with_config
    def path(self, args, config):
        self.ctx.out(config.filename)

    @with_rw_config
    def lock(self, args, config):
        self.ctx.input("Press enter to unlock")

    @with_rw_config
    def upgrade(self, args, config):
Esempio n. 14
0
def process_image(conn, imageId, parameterMap):
    """
    Process an image.
    Create a 5D image representing the ROI "cropping" the
    original image
    Image is put in a dataset if specified.
    """

    image = conn.getObject("Image", imageId)
    if image is None:
        print "No image found for ID: %s" % imageId
        return

    parentDataset = image.getParent()
    parentProject = parentDataset.getParent()

    imageName = image.getName()
    updateService = conn.getUpdateService()
    print image.countImportedImageFiles()
    # specifically count Fileset files
    fileCount = image.countFilesetFiles()
    fileset = image.getFileset() # will be None for pre-FS images
    fsId = fileset.getId()
    # list files
    if fileCount > 0:
        for origFile in image.getImportedImageFiles():
            name = origFile.getName()
            path = REPO + origFile.getPath()
    slide_name,slide_ext = os.path.splitext(name)        
    slide_path = os.path.abspath(path+name)
    print slide_path 
    slide = SlideImage(slide_path)

    # x, y, w, h, zStart, zEnd, tStart, tEnd
    rois = getRectangles(conn, imageId)

    imgW = image.getSizeX()
    imgH = image.getSizeY()
    
    for index, r in enumerate(rois):
        x, y, w, h, z1, z2, t1, t2 = r
        # Bounding box
        X = max(x, 0)
        Y = max(y, 0)
        X2 = min(x + w, imgW)
        Y2 = min(y + h, imgH)

        W = X2 - X
        H = Y2 - Y
        if (x, y, w, h) != (X, Y, W, H):
            print "\nCropping ROI (x, y, w, h) %s to be within image."\
                " New ROI: %s" % ((x, y, w, h), (X, Y, W, H))
            rois[index] = (X, Y, W, H, z1, z2, t1, t2)

    print "rois"
    print rois
    
    if len(rois) == 0:
        print "No rectangular ROIs found for image ID: %s" % imageId
        return

#make a new 5D image per ROI
    images = []
    iIds = []
    
    for i,r in enumerate(rois):
        
        x, y, w, h, z1, z2, t1, t2 = r
        print "  ROI x: %s y: %s w: %s h: %s z1: %s z2: %s t1: %s t2: %s"\
            % (x, y, w, h, z1, z2, t1, t2)
            
        description = "Image from ROIS on parent Image:\n  Name: %s\n"\
            "  Image ID: %d" % (imageName, imageId)
        print description
        
        tmp_path = create_path('omero','.tmp', folder=True)
        if (i + 1) < 10:
            new_image_name = slide_name + '_' + '00' + str(i + 1) + '.ome.tif'
            outfilepath = tmp_path + '/' + new_image_name
        elif (i + 1) >= 10:
            new_image_name = slide_name + '_' + '0' + str(i + 1) + '.ome.tif'
            outfilepath = tmp_path + '/' + new_image_name
        print 'new image path', outfilepath
        s = time.time()
        OMETIFF(None, name, outfilepath, imageId, len(rois), slide, r, 0).process()
        print 'pylibtiff took:',time.time()-s,'seconds'
        p = time.time()
        newImg = create_image_from_tiles(conn,image,new_image_name,outfilepath,description,r)
        print 'tile uploading creation took:',time.time()-p,'seconds'
#         delete_roi_image(outfilepath)
        remove_path(outfilepath)
        images.append(newImg)
        iIds.append(newImg.getId())
        
    if slide:
        slide.close_slide()
        
    if len(iIds) == 0:
        print "No new images created."
        return

    if 'Container_Name' in parameterMap and \
       len(parameterMap['Container_Name'].strip()) > 0:
        # create a new dataset for new images
        datasetName = parameterMap['Container_Name']
        dataset = omero.model.DatasetI()
        dataset.name = rstring(datasetName)
        desc = "Images in this Dataset are from ROIs of parent Image:\n"\
            "  Name: %s\n  Image ID: %d" % (imageName, imageId)
        dataset.description = rstring(desc)
        dataset = updateService.saveAndReturnObject(dataset)
        parentDataset = dataset
    else:
        # put new images in existing dataset
        dataset = None
        if parentDataset is not None and parentDataset.canLink():
            parentDataset = parentDataset._obj
        else:
            parentDataset = None
        parentProject = None    # don't add Dataset to parent.

    if parentDataset is None:
        link = None
        print "No dataset created or found for new images."\
            " Images will be orphans."
    else:
        link = []
        for iid in iIds:
            dsLink = omero.model.DatasetImageLinkI()
            dsLink.parent = omero.model.DatasetI(
                parentDataset.id.val, False)
            dsLink.child = omero.model.ImageI(iid, False)
            updateService.saveObject(dsLink)
            link.append(dsLink)
        if parentProject and parentProject.canLink():
            # and put it in the   current project
            projectLink = omero.model.ProjectDatasetLinkI()
            projectLink.parent = omero.model.ProjectI(
                parentProject.getId(), False)
            projectLink.child = omero.model.DatasetI(
                dataset.id.val, False)
            updateService.saveAndReturnObject(projectLink)
    return images, dataset, link, iIds
Esempio n. 15
0
 def testBasicUsage(self):
     p = t_f.create_path("foo", ".bar")
     assert p.exists()
     t_f.remove_path(p)
     assert not p.exists()
    def importFile(self, fileName, exName):
        """
            Import file or directory using 'bin/omero importer'
            This method is solely responsible for logging the user in,
            attempting (possibly multiply) an import, logging and
            throwing an exception if necessary.
        """

        try:
            self.state.appropriateWait(self.throttleImport)  # See ticket:5739

            key = self.loginUser(exName)
            if not key:
                self.log.info("File not imported: %s", fileName)
                return
            self.log.info("Importing %s (session=%s)", fileName, key)

            imageId = []

            t = create_path("dropbox", "err")
            to = create_path("dropbox", "out")

            cli = omero.cli.CLI()
            cli.loadplugins()
            cmd = ["-s", self.host, "-p", str(self.port), "-k", key, "import"]
            cmd.extend(
                [str("---errs=%s" % t),
                    str("---file=%s" % to), "--", "--agent=dropbox"])
            cmd.extend(shlex.split(self.importArgs))
            cmd.append(fileName)
            logging.debug("cli.invoke(%s)" % cmd)
            cli.invoke(cmd)
            retCode = cli.rv

            if retCode == 0:
                self.log.info(
                    "Import of %s completed (session=%s)", fileName, key)
                if to.exists():
                    f = open(str(to), "r")
                    lines = f.readlines()
                    f.close()
                    if len(lines) > 0:
                        for line in lines:
                            imageId.append(line.strip())
                    else:
                        self.log.error("No lines in output file. No image ID.")
                else:
                    self.log.error("%s not found !" % to)

            else:
                self.log.error(
                    "Import of %s failed=%s (session=%s)",
                    fileName, str(retCode), key)
                self.log.error(
                    "***** start of output from importer-cli to stderr *****")
                if t.exists():
                    f = open(str(t), "r")
                    lines = f.readlines()
                    f.close()
                    for line in lines:
                        self.log.error(line.strip())
                else:
                    self.log.error("%s not found !" % t)
                self.log.error("***** end of output from importer-cli *****")
        finally:
            remove_path(t)
            remove_path(to)

        return imageId
Esempio n. 17
0
 def testBasicUsagePassString(self):
     p = t_f.create_path("foo", ".bar")
     assert p.exists()
     t_f.remove_path(str(p))
     assert not p.exists()
 def testBasicUsage(self):
     p = t_f.create_path("foo", ".bar")
     assert p.exists()
     t_f.remove_path(p)
     assert not p.exists()
 def testBasicUsagePassString(self):
     p = t_f.create_path("foo", ".bar")
     assert p.exists()
     t_f.remove_path(str(p))
     assert not p.exists()