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 table data for images.""" project_id = request.GET.get('project') dataset_id = request.GET.get('dataset') plate_id = request.GET.get('plate') field_id = request.GET.get('field') logger.debug('Project:%s Dataset: %s Plate:%s Field:%s' % (project_id, dataset_id, plate_id, field_id)) if project_id is not None: img_ids = get_project_image_ids(conn, project_id) elif dataset_id is not None: img_ids = get_dataset_image_ids(conn, dataset_id) elif plate_id is not None and field_id is not None: # dict of well_id: img_id img_ids = get_well_image_ids(conn, plate_id, field_id) else: return dict() if data_name.startswith("Table_"): column_name = data_name.replace("Table_", "") table = None if project_id is not None: index_column_name = 'Image' table = get_table(conn, 'Project', project_id) if dataset_id is not None: index_column_name = 'Image' table = get_table(conn, 'Dataset', dataset_id) if plate_id is not None: index_column_name = 'Well' table = get_table(conn, 'Screen.plateLinks.child', plate_id) if table is None: table = get_table(conn, 'Plate', plate_id) if table is None: return dict() headers = table.getHeaders() column_names = [column.name for column in headers] logger.debug('Column names: %r' % column_names) index_column_index = column_names.index(index_column_name) named_column_index = column_names.index(column_name) # Load appropriate index column and named column number_of_rows = table.getNumberOfRows() logger.debug('Number of rows: %d' % number_of_rows) column_data = table.read([index_column_index, named_column_index], 0, number_of_rows).columns table_data = {} index_ids = column_data[0].values values = column_data[1].values for index_id, value in zip(index_ids, values): if project_id is not None or dataset_id is not None: table_data[index_id] = value if plate_id is not None: try: table_data[img_ids[index_id]] = value except KeyError: # The table may have data from different plates in it. We # only have a dictionary of well_id: img_id for the # current plate. pass return table_data
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') field_id = request.GET.get('field') image_ids = request.GET.getlist('image') if project_id: img_ids = get_project_image_ids(conn, project_id) elif 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() # 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 values = roi_counts.values() min_count = 0 max_count = 0 if len(values) > 0: min_count = min(values) max_count = max(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_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, })