Skip to content

Commit

Permalink
Merge branch 'ome:master' into rotate-90
Browse files Browse the repository at this point in the history
  • Loading branch information
Rdornier authored Oct 24, 2024
2 parents cb28302 + 24444fe commit d7154f5
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 26 deletions.
134 changes: 118 additions & 16 deletions omero_figure/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#
# Copyright (c) 2014-2020 University of Dundee.
#
Expand Down Expand Up @@ -56,6 +55,7 @@
logger = logging.getLogger(__name__)

JSON_FILEANN_NS = "omero.web.figure.json"
LINK_FIGURE_NS = "omero.web.figure.link"
SCRIPT_PATH = "/omero/figure_scripts/Figure_To_Pdf.py"


Expand Down Expand Up @@ -164,8 +164,8 @@ def max_projection_range_exceeded(request, iid, z=None, t=None,

im = Image.new("RGB", (image_size, image_size), (5, 0, 0))
draw = ImageDraw.Draw(im)
text_y = im.size[1]/2 - txt_h/2
draw.text((im.size[0]/2 - txt_w/2, text_y), msg,
text_y = im.size[1] / 2 - txt_h / 2
draw.text((im.size[0] / 2 - txt_w / 2, text_y), msg,
font=font20,
fill=(256, 256, 256))

Expand All @@ -176,7 +176,6 @@ def max_projection_range_exceeded(request, iid, z=None, t=None,

@login_required()
def img_data_json(request, image_id, conn=None, **kwargs):

image = conn.getObject("Image", image_id)
if image is None:
raise Http404("Image not found")
Expand Down Expand Up @@ -219,7 +218,6 @@ def img_data_json(request, image_id, conn=None, **kwargs):

@login_required()
def timestamps(request, conn=None, **kwargs):

iids = request.GET.getlist('image')
data = {}
for iid in iids:
Expand All @@ -231,7 +229,6 @@ def timestamps(request, conn=None, **kwargs):

@login_required()
def pixels_type(request, conn=None, **kwargs):

iids = request.GET.getlist('image')
data = {}
for iid in iids:
Expand All @@ -251,7 +248,6 @@ def pixels_type(request, conn=None, **kwargs):

@login_required()
def z_scale(request, conn=None, **kwargs):

iids = request.GET.getlist('image')
data = {}
for iid in iids:
Expand All @@ -267,7 +263,6 @@ def z_scale(request, conn=None, **kwargs):

@login_required()
def render_scaled_region(request, iid, z, t, conn=None, **kwargs):

region = request.GET.get('region')
logger.debug("Rendering region: %s, Image: %s" % (region, iid))

Expand Down Expand Up @@ -385,7 +380,7 @@ def save_web_figure(request, conn=None, **kwargs):
n = datetime.now()
# time-stamp name by default: WebFigure_2013-10-29_22-43-53.json
figure_name = "Figure_%s-%s-%s_%s-%s-%s.json" % \
(n.year, n.month, n.day, n.hour, n.minute, n.second)
(n.year, n.month, n.day, n.hour, n.minute, n.second)

# we store json in description field...
description = {}
Expand Down Expand Up @@ -423,7 +418,6 @@ def save_web_figure(request, conn=None, **kwargs):
fa.setDescription(wrap(desc))
fa = update.saveAndReturnObject(fa, conn.SERVICE_OPTS)
file_id = fa.getId().getValue()

else:
# Update existing Original File
fa = conn.getObject("FileAnnotation", file_id)
Expand All @@ -445,14 +439,25 @@ def save_web_figure(request, conn=None, **kwargs):
raw_file_store.setFileId(orig_file.getId().getValue(),
conn.SERVICE_OPTS)
raw_file_store.write(figure_json, 0, size, conn.SERVICE_OPTS)
raw_file_store.truncate(size, conn.SERVICE_OPTS) # ticket #11751
raw_file_store.truncate(size, conn.SERVICE_OPTS) # ticket #11751
# Once #11928 is fixed, these last 2 lines can be replaced with
# rawFileStore.close(conn.SERVICE_OPTS)
raw_file_store.save(conn.SERVICE_OPTS)
raw_file_store.close()

# create a new key-value pair for the new figure
figure_url = request.build_absolute_uri(reverse('load_figure',
args=[file_id]))
map_id = create_or_get_figure_kvp(conn, LINK_FIGURE_NS,
figure_name, file_id, figure_url)

# Link figure URL as key-value pair to all images (remove from any others)
if map_id > 0:
link_figure_kvp_to_images(conn, update, LINK_FIGURE_NS,
image_ids, map_id)

# Link file annotation to all images (remove from any others)
link_to_images = False # Disabled for now
link_to_images = False # Disabled for now
if link_to_images:
current_links = conn.getAnnotationLinks("Image", ann_ids=[file_id])
for link in current_links:
Expand Down Expand Up @@ -482,6 +487,105 @@ def save_web_figure(request, conn=None, **kwargs):
return HttpResponse(str(file_id))


def delete_figure_kvp(conn, ns, file_id):
# get kvp
ann = get_figure_kvp(conn, ns, file_id)
if ann is not None and len(ann) > 0:
map_ann = ann[0]
map_id = map_ann[0]
update = conn.getUpdateService()

# get the links between the key-value and the linked images
current_links = conn.getAnnotationLinks(
"Image",
ns=(wrap(ns)),
ann_ids=[map_id]
)
# delete links
for link in current_links:
update.deleteObject(link._obj, conn.SERVICE_OPTS)

# delete kvp
conn.deleteObjects("Annotation", [map_id])


def link_figure_kvp_to_images(conn, update, ns, image_ids, map_id):
# get the links between the key-value and the linked images
current_links = conn.getAnnotationLinks(
"Image",
ns=(wrap(ns)),
ann_ids=[map_id]
)
for link in current_links:
if link.getParent().getId() not in image_ids:
# remove old link
update.deleteObject(link._obj, conn.SERVICE_OPTS)
else:
# we don't need to create links for these
image_ids.remove(link.getParent().getId())

# create new links if necessary
links = []
if len(image_ids) > 0:
for i in conn.getObjects("Image", image_ids):
if not i.canAnnotate():
continue
link = omero.model.ImageAnnotationLinkI()
link.parent = omero.model.ImageI(i.getId(), False)
link.child = omero.model.MapAnnotationI(map_id, False)
links.append(link)
# Don't want to fail at this point due to strange permissions combo
try:
update.saveArray(links, conn.SERVICE_OPTS)
except Exception:
pass


def create_or_get_figure_kvp(conn, ns, figure_name, file_id, figure_url):
# retrieve the key-value corresponding to the current figure
try:
ann = get_figure_kvp(conn, ns, file_id)

if ann is not None and len(ann) > 0:
map_att = ann[0]
map_name = map_att[1]
if figure_name in map_name:
return map_att[0]

map_ann = conn.getObject('MapAnnotation', map_att[0])
else:
map_ann = omero.gateway.MapAnnotationWrapper(conn)
map_ann.setNs(wrap(ns))

map_ann.setValue([["Figure_%s_%s" % (file_id, figure_name),
figure_url]])
map_ann.save()

return map_ann.getId()
except Exception:
return -1


def get_figure_kvp(conn, ns, file_id):
params = omero.sys.ParametersI()
where_clause = []
where_clause.append(f"mv.name like 'Figure_{file_id}%'")

params.add('ns', wrap([ns]))
where_clause.append("a.ns in (:ns)")
where_clause.append("mv.value != '' ")

qs = conn.getQueryService()
q = """
select distinct a.id, mv.name
from Annotation a
join a.mapValue mv where %s
""" % (" and ".join(where_clause))

results = qs.projection(q, params, conn.SERVICE_OPTS)
return [[result[0].val, result[1].val] for result in results]


@login_required()
def load_web_figure(request, file_id, conn=None, **kwargs):
"""
Expand Down Expand Up @@ -557,7 +661,6 @@ def make_web_figure(request, conn=None, **kwargs):

@login_required()
def list_web_figures(request, conn=None, **kwargs):

params = omero.sys.ParametersI()
params.addString('ns', rstring(JSON_FILEANN_NS))
q = """select new map(obj.id as id,
Expand All @@ -582,7 +685,7 @@ def list_web_figures(request, conn=None, **kwargs):
rsp = []
for file_ann in file_anns:
fa = unwrap(file_ann[0])
date = datetime.fromtimestamp(unwrap(fa['time'])/1000)
date = datetime.fromtimestamp(unwrap(fa['time']) / 1000)
first_name = unwrap(fa['firstName'])
last_name = unwrap(fa['lastName'])
fig_file = {
Expand Down Expand Up @@ -628,6 +731,7 @@ def delete_web_figure(request, conn=None, **kwargs):
return HttpResponse("Need to POST 'fileId' to delete")

file_id = request.POST.get('fileId')
delete_figure_kvp(conn, LINK_FIGURE_NS, file_id)
conn.deleteObjects("Annotation", [file_id])
return HttpResponse("Deleted OK")

Expand Down Expand Up @@ -737,7 +841,6 @@ def roi_count(request, image_id, conn=None, **kwargs):
@require_POST
@login_required()
def chgrp(request, conn=None, **kwargs):

group_id = int(request.POST.get("group_id"))
ann_id = int(request.POST.get("ann_id"))

Expand Down Expand Up @@ -768,7 +871,6 @@ def chgrp(request, conn=None, **kwargs):

@login_required()
def images_details(request, conn=None, **kwargs):

imgs = request.GET.get('image', '')
img_ids = [int(i) for i in imgs.split(',') if len(i) > 0]

Expand Down
10 changes: 5 additions & 5 deletions src/js/models/figure_model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import Backbone from "backbone";
import _ from 'underscore';
import $ from "jquery";
Expand Down Expand Up @@ -52,7 +52,7 @@
});

// wrap selection notification in a 'debounce', so that many rapid
// selection changes only trigger a single re-rendering
// selection changes only trigger a single re-rendering
this.notifySelectionChange = _.debounce( this.notifySelectionChange, 10);
},

Expand Down Expand Up @@ -237,7 +237,7 @@
});
}
}

if (v < 6) {
console.log("Transforming to VERSION 6");
// Adding the Z scale to the model
Expand Down Expand Up @@ -483,7 +483,7 @@
doClear();
}
},

addImages: function(iIds) {
this.clearSelected();

Expand Down Expand Up @@ -702,7 +702,7 @@
});
},


align_right: function() {
var selected = this.getSelected(),
x_vals = [];
Expand Down
10 changes: 5 additions & 5 deletions src/js/views/figure_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@
var msg = "Delete '" + figName + "'?";
var self = this;
if (confirm(msg)) {
$.post( BASE_WEBFIGURE_URL + "delete_web_figure/", { fileId: fileId })
$.post( BASE_WEBFIGURE_URL + "delete_web_figure/", { fileId: fileId})
.done(function(){
self.figureFiles.removeFile(fileId);
self.app.navigate("", {trigger: true});
Expand Down Expand Up @@ -544,7 +544,7 @@
event.preventDefault();
showExportAsJsonModal(this.model.figure_toJSON());
},

import_json: function(event) {
event.preventDefault();

Expand All @@ -556,15 +556,15 @@

import_json_form: function(event) {
event.preventDefault();

var figureJSON = $('.importJsonForm .form-control').val();
this.model.figure_fromJSON(figureJSON);

hideModal('importJsonModal');
$('#importJsonModal textarea').val('');
this.render();
},

copy_selected_panels: function(event) {
event.preventDefault();
if (this.modal_visible()) return true;
Expand Down

0 comments on commit d7154f5

Please sign in to comment.