Exemplo n.º 1
0
def create_roi(image, shapes):
    updateService = image._conn.getUpdateService()
    roi = RoiI()
    roi.setImage(image._obj)
    for shape in shapes:
        roi.addShape(shape)
    return updateService.saveAndReturnObject(roi)
Exemplo n.º 2
0
    def image_rois(self, user1, shapes):
        """Return Image with ROIs."""
        image = ImageI()
        image.name = rstring('Image for ROIs')
        image = get_update_service(user1).saveAndReturnObject(image)

        # ROI with all but one shapes
        rois = []
        roi = RoiI()
        for shape in shapes[:-1]:
            roi.addShape(shape)
        roi.setImage(image)
        rois.append(roi)

        # roi without shapes
        roi = RoiI()
        roi.setImage(image)
        rois.append(roi)

        # roi without image
        roi = RoiI()
        roi.addShape(shapes[-1])
        rois.append(roi)

        rois = get_update_service(user1).saveAndReturnArray(rois)
        rois.sort(key=lambda x: x.id.val)
        return image, rois
Exemplo n.º 3
0
def create_roi(conn, img, shapes):
    updateService = conn.getUpdateService()
    roi = RoiI()
    roi.setImage(img._obj)
    for shape in shapes:
        roi.addShape(shape)
    group_id = img.getDetails().getGroup().getId()
    print(group_id)
    ctx = {'omero.group': str(group_id)}
    return updateService.saveAndReturnObject(roi, ctx)
Exemplo n.º 4
0
    def addRoi(self, request, imageid):
        img = self.getImage0(imageid)
        if img is None:
            return None

        data = json.loads(request.body)
        shapes = data['shapes']
        x = y = l = z = t = -1
        for s in shapes:
            for k in s:
                val = s[k]
                if k == "x":
                    x = int(val)
                elif k == "y":
                    y = int(val)
                elif k == "width":
                    l = int(val)
                elif k == "theZ":
                    z = int(val)
                elif k == "theT":
                    t = int(val)
                elif k == "fillColorAsInt":
                    fill = int(val)
                elif k == "strokeColorAsInt":
                    stroke = int(val)
                elif k == "strokeWidth":
                    strokeWidth = int(val)

        if (x < 0 or y < 0 or z < 0 or t < 0 or l <= 0):
            return None

        updateService = self._connection.getUpdateService()
        roi = RoiI()
        roi.setImage(img._obj)
        rect = RectangleI()
        rect.x = rdouble(x)
        rect.y = rdouble(y)
        rect.width = rdouble(l)
        rect.height = rdouble(l)
        rect.theZ = rint(z)
        rect.theT = rint(t)
        rect.setFillColor(rint(fill))
        strokeLen = LengthI()
        strokeLen.setValue(strokeWidth)
        strokeLen.setUnit(UnitsLength.PIXEL)
        rect.setStrokeWidth(strokeLen)
        rect.setStrokeColor(rint(stroke))
        roi.addShape(rect)

        if (updateService.saveAndReturnObject(roi) is None):
            return None
        return self.get_rois(imageid)
Exemplo n.º 5
0
    def addRoi(self, request, imageid):
        img = self.getImage0(imageid)
        if img is None:
            return None

        data = json.loads(request.body)
        shapes = data['shapes']
        x = y = l = z = t = -1
        for s in shapes:
            for k in s:
                val = s[k]
                if k == "x":
                    x = int(val)
                elif k == "y":
                    y = int(val)
                elif k == "width":
                    l = int(val)
                elif k == "theZ":
                    z = int(val)
                elif k == "theT":
                    t = int(val)
                elif k == "fillColorAsInt":
                    fill = int(val)
                elif k == "strokeColorAsInt":
                    stroke = int(val)
                elif k == "strokeWidth":
                    strokeWidth = int(val)
        
        if (x < 0 or y < 0 or z < 0 or t < 0 or l <= 0):
            return None
        
        updateService = self._connection.getUpdateService()
        roi = RoiI()
        roi.setImage(img._obj)
        rect = RectangleI()
        rect.x = rdouble(x)
        rect.y = rdouble(y)
        rect.width = rdouble(l)
        rect.height = rdouble(l)
        rect.theZ = rint(z)
        rect.theT = rint(t)
        rect.setFillColor(rint(fill))
        strokeLen = LengthI()
        strokeLen.setValue(strokeWidth)
        strokeLen.setUnit(UnitsLength.PIXEL)
        rect.setStrokeWidth(strokeLen)
        rect.setStrokeColor(rint(stroke))
        roi.addShape(rect)
        
        if (updateService.saveAndReturnObject(roi) is None):
            return None
        return self.get_rois(imageid) 
def process_file(inputfile, image, svc, offset):
    """
    Read each line of the file
    """
    parents = dict()
    line_count = 0
    roi = None
    parent_shape_id = -1
    parent_id = -1
    cell_id = 0
    x = image.getSizeX() / 2
    y = 0
    if offset is not None:
        y = image.getSizeY() - int(offset[1])

    last_frame = 0
    with open(inputfile) as fp:
        csv_reader = csv.reader(fp, delimiter="\t", quotechar='"')
        for row in csv_reader:
            if line_count > 1:
                # Check the first element to parse
                name = row[0]
                if name.find(">CELL") >= 0:
                    # extract cell info
                    if roi is not None:
                        # we save the roi before starting a new shape
                        roi = svc.saveAndReturnObject(roi)
                        parents[cell_id] = get_last_shape(roi, last_frame)
                    last_frame = -1
                    cell_id = int(row[1])
                    parent_id = int(row[2])
                    end_type = row[6]
                    roi = RoiI()
                    roi.setImage(image._obj)
                    value = "cellID:%s, parent_id: %s" % (cell_id, parent_id)
                    print(value)
                    if parent_id != -1:
                        parent_shape_id = parents[parent_id]
                    else:
                        parent_shape_id = -1
                else:
                    last_frame = process_row(row, x, y, roi, end_type,
                                             parent_shape_id)
                    parent_shape_id = -1
            line_count += 1
Exemplo n.º 7
0
def create_points(conn, df, image):

    columns = [
        "Sequence", "Tagged Protein", "Sequence is unique", "Location_X",
        "Location_Y"
    ]
    df2 = pandas.DataFrame(columns=(['Roi'] + columns))
    index = df['Source Name'] == image.getName()
    for (s, tp, u, x, y) in zip(*map(lambda x: df[index][x], columns)):
        p = PointI()
        p.x = rdouble(float(x))
        p.y = rdouble(float(y))
        p.setTextValue(rstring(tp))
        roi = RoiI()
        roi.addShape(p)
        roi.setName(rstring(tp))
        roi.setImage(ImageI(image.getId(), False))
        roi = conn.getUpdateService().saveAndReturnObject(roi)
        df2.loc[len(df2)] = (roi.getId().getValue(), s, tp, u, x, y)
    return df2
    def test_roi_thumbnail(self, theT, theZ):
        update_service = self.sf.getUpdateService()
        img = self.create_test_image(size_x=125,
                                     size_y=125,
                                     size_z=2,
                                     size_t=2,
                                     session=self.sf)

        for s in self.shapes():
            if theT is not None:
                s.theT = rint(theT)
            if theZ is not None:
                s.theZ = rint(theZ)
            roi = RoiI()
            roi.addShape(s)
            roi.setImage(img)
            roi = update_service.saveAndReturnObject(roi)
            shape = roi.copyShapes()[0]

            # Test ROI thumbnail...
            request_url = reverse('webgateway_render_roi_thumbnail',
                                  kwargs={'roiId': roi.id.val})
            rsp = get(self.django_client, request_url)
            thumb_bytes = BytesIO(rsp.content)
            try:
                thumb = Image.open(thumb_bytes)
            finally:
                thumb_bytes.close()
            assert thumb.size == (250, 166)

            # and Shape thumbnail...
            request_url = reverse('webgateway_render_shape_thumbnail',
                                  kwargs={'shapeId': shape.id.val})
            rsp = get(self.django_client, request_url)
            thumb_bytes = BytesIO(rsp.content)
            try:
                thumb = Image.open(thumb_bytes)
            finally:
                thumb_bytes.close()
            assert thumb.size == (250, 166)
Exemplo n.º 9
0
    def test_save_rois(self, conn, django_client):
        """Save new ROIs to an Image"""
        image = self.make_image(client=conn.c)
        roi = RoiI()
        roi.name = rstring("roi_name")
        roi.setImage(ImageI(image.id.val, False))
        point = PointI()
        point.x = rdouble(1)
        point.y = rdouble(2)

        encoder = get_encoder(point.__class__)
        point_json = encoder.encode(point)
        unsaved_id = "-1:-1"
        point_json['oldId'] = unsaved_id
        persist_url = reverse('omero_iviewer_persist_rois')
        data = {
            'imageId': image.id.val,
            'rois': {
                'count': 1,
                'new': [point_json]
            }
        }
        rsp = post_json(django_client, persist_url, data)
        print('rsp', rsp)
        # {"ids": {"-1:-1": "225504:416603"}}
        assert "ids" in rsp
        new_ids = rsp["ids"].values()
        assert len(new_ids) == 1
        new_id = rsp["ids"][unsaved_id]
        roi_id = int(new_id.split(':')[0])
        shape_id = int(new_id.split(':')[1])

        # Add Tag to ROI and Shape
        tag = TagAnnotationWrapper(conn)
        tag.setValue("ROI/Shape Tag")
        tag.save()
        roi = conn.getObject("Roi", roi_id)
        roi.linkAnnotation(tag)
        shape = conn.getObject("Shape", shape_id)
        shape.linkAnnotation(tag)
        # check...
        assert len(list(conn.getAnnotationLinks(
            "Shape", parent_ids=[shape_id]))) == 1

        # Load Shape
        rois_url = reverse('api_rois', kwargs={'api_version': 0})
        rois_url += '?image=%s' % image.id.val
        rsp = get_json(django_client, rois_url)
        assert len(rsp['data']) == 1

        # Edit Shape
        point_json = rsp['data'][0]['shapes'][0]
        point_json["X"] = 100
        point_json["Y"] = 200
        # iviewer wants to know ROI:Shape ID
        point_json["oldId"] = new_id
        # Unload details
        del point_json["omero:details"]
        data = {
            'imageId': image.id.val,
            'rois': {
                'count': 1,
                'modified': [point_json]
            }
        }
        rsp = post_json(django_client, persist_url, data)
        # IDs shouldn't have changed, e.g.
        # {"ids": {"225504:416603": "225504:416603"}}
        print('post rsp', rsp)
        assert rsp["ids"][new_id] == new_id

        # Check annotations not lost
        roi = conn.getObject("Roi", roi_id)
        assert len(list(roi.listAnnotations())) == 1
        assert len(list(conn.getAnnotationLinks(
            "Shape", parent_ids=[shape_id]))) == 1