예제 #1
0
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,
        })
예제 #2
0
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,
        })