def get_script(request, script_name, conn): """Return a JS function to filter images by various params.""" dataset_id = request.GET.get('dataset') plate_id = request.GET.get('plate') field_id = request.GET.get('field') image_ids = request.GET.getlist('image') if plate_id and field_id: img_ids = get_image_ids(conn, plate_id, field_id) elif dataset_id: objects = conn.getObjects('Image', opts={'dataset': dataset_id}) img_ids = [i.id for i in objects] else: img_ids = [long(i) for i in image_ids] query_service = conn.getQueryService() if script_name == "ROI_count": # Want to get ROI count for images in plate # Get ROI counts params = ParametersI() params.addIds(img_ids) query = "select roi.image.id, count(roi.id) from Roi roi "\ "where roi.image.id in (:ids) group by roi.image" p = query_service.projection(query, params, conn.SERVICE_OPTS) roi_counts = {} for i in p: roi_counts[i[0].val] = i[1].val min_count = min(roi_counts.values()) max_count = max(roi_counts.values()) # Return a JS function that will be passed an object # e.g. {'type': 'Image', 'id': 1} # and should return true or false f = """(function filter(data, params) { var roi_counts = %s; if (isNaN(params.count) || params.count == '') return true; if (params.operator === '=') return roi_counts[data.id] == params.count; if (params.operator === '<') return roi_counts[data.id] < params.count; if (params.operator === '>') return roi_counts[data.id] > params.count; }) """ % json.dumps(roi_counts) filter_params = [ {'name': 'operator', 'type': 'text', 'values': ['>', '=', '<'], 'default': '>'}, {'name': 'count', 'type': 'number', 'default': '', 'title': '%s-%s' % (min_count, max_count)} ] return JsonResponse( { 'f': f, 'params': filter_params, })
def get_data(request, data_name, conn): """Return data for images in a Project, Dataset or Plate.""" project_id = request.GET.get('project') dataset_id = request.GET.get('dataset') plate_id = request.GET.get('plate') field_id = request.GET.get('field') if project_id: img_ids = get_project_image_ids(conn, project_id) elif dataset_id: objects = conn.getObjects('Image', opts={'dataset': dataset_id}) img_ids = [i.id for i in objects] elif plate_id and field_id: img_ids = get_image_ids(conn, plate_id, field_id) else: img_ids = request.GET.getlist('image') query_service = conn.getQueryService() if data_name == "ROI_stats_max_size": if plate_id: ns = "roi.pixel.intensities.summary" return get_image_map_annotations(conn, plate_id, 0, ns, "Max Points") if data_name == "ROI_count": # Want to get ROI count for images params = ParametersI() # Include "-1" so that if we have no Image IDs that the query does # not fail. It will not match anything. params.addIds([-1] + img_ids) query = "select roi.image.id, count(roi.id) from Roi roi "\ "where roi.image.id in (:ids) group by roi.image" p = query_service.projection(query, params, conn.SERVICE_OPTS) roi_counts = {} for i in img_ids: # Add placeholder 0 for all images roi_counts[i] = 0 for i in p: roi_counts[i[0].val] = i[1].val return roi_counts if data_name == "sizeT": # Want to get sizeT for images params = ParametersI() # Include "-1" so that if we have no Image IDs that the query does # not fail. It will not match anything. params.addIds([-1] + img_ids) query = "select pixels.image.id, pixels.sizeT from Pixels pixels "\ "where pixels.image.id in (:ids)" p = query_service.projection(query, params, conn.SERVICE_OPTS) size_t = {} for i in p: size_t[i[0].val] = i[1].val return size_t
def get_data(request, data_name, conn): """Return data for images in a Dataset or Plate.""" dataset_id = request.GET.get('dataset') plate_id = request.GET.get('plate') field_id = request.GET.get('field') if plate_id and field_id: img_ids = get_image_ids(conn, plate_id, field_id) elif dataset_id: objects = conn.getObjects('Image', opts={'dataset': dataset_id}) img_ids = [i.id for i in objects] query_service = conn.getQueryService() if data_name == "ROI_stats_max_size": if plate_id: ns = "roi.pixel.intensities.summary" return get_image_map_annotations(conn, plate_id, 0, ns, "Max Points") if data_name == "ROI_count": # Want to get ROI count for images params = ParametersI() params.addIds(img_ids) query = "select roi.image.id, count(roi.id) from Roi roi "\ "where roi.image.id in (:ids) group by roi.image" p = query_service.projection(query, params, conn.SERVICE_OPTS) roi_counts = {} for i in p: roi_counts[i[0].val] = i[1].val return roi_counts if data_name == "sizeT": # Want to get sizeT for images params = ParametersI() params.addIds(img_ids) query = "select pixels.image.id, pixels.sizeT from Pixels pixels "\ "where pixels.image.id in (:ids)" p = query_service.projection(query, params, conn.SERVICE_OPTS) size_t = {} for i in p: size_t[i[0].val] = i[1].val return size_t
def get_image_map_annotations(conn, plate_id, field_id, ns, key=None): """Get image IDs for images in Plate""" conn.SERVICE_OPTS.setOmeroGroup('-1') query_service = conn.getQueryService() iids = get_image_ids(conn, plate_id, field_id) params = ParametersI() params.addIds(iids) query = """select oal from ImageAnnotationLink as oal join fetch oal.details.owner left outer join fetch oal.child as ch left outer join fetch oal.parent as pa where pa.id in (:ids) and ch.ns='%s'""" % ns links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if key is None: return links map_values = {} for l in links: for kv in l.child.getMapValue(): if key == kv.name: map_values[l.parent.id.val] = long(kv.value) return map_values
def get_script(request, script_name, conn): """Return a JS function to filter images by various params.""" dataset_id = request.GET.get('dataset') plate_id = request.GET.get('plate') dtype = "Image" js_object_attr = "id" if dataset_id: objects = conn.getObjects('Image', opts={'dataset': dataset_id}) obj_ids = [i.id for i in objects] elif plate_id: dtype = "Well" js_object_attr = "wellId" obj_ids = get_well_ids(conn, plate_id) query_service = conn.getQueryService() if script_name == "Rating": params = ParametersI() params.addIds(obj_ids) query = """select oal from %sAnnotationLink as oal join fetch oal.details.owner left outer join fetch oal.child as ch left outer join fetch oal.parent as pa where pa.id in (:ids) and ch.class=LongAnnotation and ch.ns='openmicroscopy.org/omero/insight/rating'""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) ratings = {} for l in links: ratings[l.parent.id.val] = l.child.longValue.val # Return a JS function that will be passed an object # e.g. {'type': 'Image', 'id': 1} # and should return true or false f = """(function filter(data, params) { var ratings = %s; index = ratings[data.%s] == params.rating return (params.rating === '-' || index); }) """ % (json.dumps(ratings), js_object_attr) filter_params = [{ 'name': 'rating', 'type': 'text', 'values': ['-', '1', '2', '3', '4', '5'], 'default': '-', }] return JsonResponse({ 'f': f, 'params': filter_params, }) if script_name == "Comment": params = ParametersI() params.addIds(obj_ids) query = """select oal from %sAnnotationLink as oal left outer join fetch oal.child as ch left outer join oal.parent as pa where pa.id in (:ids) and ch.class=CommentAnnotation""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) comments = {} for l in links: iid = l.parent.id.val if iid not in comments: comments[iid] = "" comments[iid] = " ".join([comments[iid], l.child.textValue.val]) # Return a JS function that will be passed a data object # e.g. {'type': 'Image', 'id': 1} # and a params object of {'paramName': value} # and should return true or false f = """(function filter(data, params) { var comments = %s; index = comments[data.%s] && comments[data.%s].indexOf(params.comment) > -1 return (params.comment === '' || index); }) """ % (json.dumps(comments), js_object_attr, js_object_attr) filter_params = [{ 'name': 'comment', 'type': 'text', 'default': '', }] return JsonResponse({ 'f': f, 'params': filter_params, }) if script_name == "Tag": params = ParametersI() params.addIds(obj_ids) query = """select oal from %sAnnotationLink as oal left outer join fetch oal.child as ch left outer join oal.parent as pa where pa.id in (:ids) and ch.class=TagAnnotation""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) tags = {} all_tags = [] for l in links: iid = l.parent.id.val text = l.child.textValue.val all_tags.append(text) if iid not in tags: tags[iid] = [] tags[iid].append(text) # remove duplicates all_tags = list(set(all_tags)) # Return a JS function that will be passed a data object # e.g. {'type': 'Image', 'id': 1} # and a params object of {'paramName': value} # and should return true or false f = """(function filter(data, params) { var tags = %s; index = tags[data.%s] && tags[data.%s].indexOf(params.tag) > -1 return (params.tag === 'Choose_Tag' || index); }) """ % (json.dumps(tags), js_object_attr, js_object_attr) all_tags.insert(0, "Choose_Tag") filter_params = [{ 'name': 'tag', 'type': 'text', 'default': "Choose_Tag", 'values': all_tags, }] return JsonResponse({ 'f': f, 'params': filter_params, })
def get_script(request, script_name, conn): """Return a JS function to filter images by various params.""" project_id = request.GET.get('project') dataset_id = request.GET.get('dataset') plate_id = request.GET.get('plate') image_ids = request.GET.getlist('image') dtype = "Image" js_object_attr = "id" if project_id: obj_ids = get_project_image_ids(conn, project_id) elif dataset_id: objects = conn.getObjects('Image', opts={'dataset': dataset_id}) obj_ids = [i.id for i in objects] elif plate_id: dtype = "Well" js_object_attr = "wellId" obj_ids = get_well_ids(conn, plate_id) else: obj_ids = [int(i) for i in image_ids] query_service = conn.getQueryService() params = ParametersI() # Include "-1" so that if we have no object IDs that the query does # not fail. It will not match anything. params.addIds([-1] + obj_ids) if script_name == "Rating": query = """select oal from %sAnnotationLink as oal join fetch oal.details.owner left outer join fetch oal.child as ch left outer join fetch oal.parent as pa where pa.id in (:ids) and ch.class=LongAnnotation and ch.ns='openmicroscopy.org/omero/insight/rating'""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) ratings = {} for l in links: ratings[l.parent.id.val] = l.child.longValue.val # Return a JS function that will be passed an object # e.g. {'type': 'Image', 'id': 1} # and should return true or false f = """(function filter(data, params) { var ratings = %s; var index = ratings[data.%s] == params.rating return (params.rating === '-' || index); }) """ % (json.dumps(ratings), js_object_attr) filter_params = [{ 'name': 'rating', 'type': 'text', 'values': ['-', '1', '2', '3', '4', '5'], 'default': '-', }] return JsonResponse({ 'f': f, 'params': filter_params, }) if script_name == "Comment": query = """select oal from %sAnnotationLink as oal left outer join fetch oal.child as ch left outer join oal.parent as pa where pa.id in (:ids) and ch.class=CommentAnnotation""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) comments = {} for l in links: iid = l.parent.id.val if iid not in comments: comments[iid] = "" comments[iid] = " ".join([comments[iid], l.child.textValue.val]) # Return a JS function that will be passed a data object # e.g. {'type': 'Image', 'id': 1} # and a params object of {'paramName': value} # and should return true or false f = """(function filter(data, params) { var comments = %s; var index = comments[data.%s] && comments[data.%s].indexOf(params.comment) > -1 return (params.comment === '' || index); }) """ % (json.dumps(comments), js_object_attr, js_object_attr) filter_params = [{ 'name': 'comment', 'type': 'text', 'default': '', }] return JsonResponse({ 'f': f, 'params': filter_params, }) if script_name == "Tag": query = """select oal from %sAnnotationLink as oal left outer join fetch oal.child as ch left outer join oal.parent as pa where pa.id in (:ids) and ch.class=TagAnnotation""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) tags = {} all_tags = [] for l in links: iid = l.parent.id.val text = l.child.textValue.val all_tags.append(text) if iid not in tags: tags[iid] = [] tags[iid].append(text) # remove duplicates all_tags = list(set(all_tags)) # Return a JS function that will be passed a data object # e.g. {'type': 'Image', 'id': 1} # and a params object of {'paramName': value} # and should return true or false f = """(function filter(data, params) { var tags = %s; var index = tags[data.%s] && tags[data.%s].indexOf(params.tag) > -1 return (params.tag === 'Choose_Tag' || index); }) """ % (json.dumps(tags), js_object_attr, js_object_attr) all_tags.insert(0, "Choose_Tag") filter_params = [{ 'name': 'tag', 'type': 'text', 'default': "Choose_Tag", 'values': all_tags, }] return JsonResponse({ 'f': f, 'params': filter_params, }) if script_name == "Key_Value": query = """select oal from %sAnnotationLink as oal left outer join fetch oal.child as ch left outer join oal.parent as pa where pa.id in (:ids) and ch.class=MapAnnotation""" % dtype links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) # Dict of {'key': {iid: 'value', iid: 'value'}} map_values = defaultdict(dict) for l in links: iid = l.parent.id.val for kv in l.child.getMapValue(): map_values[kv.name][iid] = kv.value key_placeholder = "Pick key..." # Return a JS function that will be passed a data object # e.g. {'type': 'Image', 'id': 1} # and a params object of {'paramName': value} # and should return true or false f = """ (function filter(data, params) { var map_values = %s; var key_placeholder = "%s"; if (params.key === key_placeholder) return true; if (map_values[params.key] && map_values[params.key][data.%s]) { var match = map_values[params.key][data.%s].indexOf(params.query) > -1; return (params.query === '' || match); } return false; }) """ % (json.dumps(map_values), key_placeholder, js_object_attr, js_object_attr) keys = list(map_values.keys()) keys.sort(key=lambda x: x.lower()) filter_params = [{ 'name': 'key', 'type': 'text', 'values': [key_placeholder] + keys, 'default': key_placeholder }, { 'name': 'query', 'type': 'text', 'default': '' }] return JsonResponse({ 'f': f, 'params': filter_params, })
rows = query_service.projection(query, params) plate_ids = [row[0].val for row in rows] assert plate_ids # Loop through each field of those plates. query = """ SELECT id, image.name, image.fileset.id, well.row, well.column, well.plate.name FROM WellSample WHERE well.plate.id IN (:ids) """ params = ParametersI() params.addIds(plate_ids) rows = query_service.projection(query, params) for row in rows: field_id = row[0].val image_name = row[1].val fileset_id = row[2].val well_row = row[3].val well_column = row[4].val plate_name = row[5].val # Figure the expected image name for this field. image_name_expected = '{} [Well {}{}, Field 1]' \ .format(plate_name, chr(65 + well_row), 1 + well_column)