def __generate_coco_annotations(img_labels: list[dict], img_id: int, annotation_first_id: int, classes: list[str]) -> list[dict]: annotations = [] for img_label in img_labels: class_name = img_label['value'] polygon = img_label['polygon'] poly_convert = [(p['x'], p['y']) for p in polygon] bbox = __bounding_rect_from_polygon(poly_convert) segmentation = list(itertools.chain.from_iterable(poly_convert)) if class_name not in classes: classes.append(class_name) annotations.append({ 'segmentation': [segmentation], 'iscrowd': 0, 'image_id': img_id, 'category_id': classes.index(class_name), 'id': annotation_first_id, 'bbox': bbox, 'area': __calculate_area(segmentation) }) annotation_first_id += 1 return annotations
def map_external_issues_to_annotations(self, external_issues): annotations = [] for ei in external_issues: link = self.get_issue_url(ei.key) label = self.get_issue_display_name(ei) or ei.key annotations.append(f'<a href="{link}">{label}</a>') return annotations
def transform_item(self, item): labels = set(p.label for p in item.annotations if getattr(p, 'label') is not None) annotations = [] for label in labels: annotations.append(Label(label=label)) return item.wrap(annotations=annotations)
def transform_item(self, item): annotations = [] for ann in item.annotations: if getattr(ann, 'label', None) is not None: conv_label = self._map_id(ann.label) if conv_label is not None: annotations.append(ann.wrap(label=conv_label)) else: annotations.append(ann.wrap()) return item.wrap(annotations=annotations)
def _get_all_annotations(node: astroid.FunctionDef) -> list[astroid.NodeNG | None]: args = node.args annotations: list[astroid.NodeNG | None] = ( args.posonlyargs_annotations + args.annotations + args.kwonlyargs_annotations ) if args.vararg is not None: annotations.append(args.varargannotation) if args.kwarg is not None: annotations.append(args.kwargannotation) return annotations
def _get_annotations(self, name_column: str): """ Returns the annotations in the specified column as Annotation objects """ annotations = list() for index, B_token in enumerate(self.tokens): token_annotation = getattr(B_token, name_column) if token_annotation: # Get the B-elements B_elements = [ element for element in token_annotation.split("#") if element.startswith("B-") ] for B_element in B_elements: # Get id and class of annotation ann_id = B_element.split(":")[0].split("-")[-1] ann_class = B_element.split("-")[1].lower() # Create tuples (id, class) for relations and add to list relations = [] if ":" in B_element: for relation in B_element.split(":")[1].split("_"): relation_id, relation_class = relation.split("-") relations.append((relation_id, relation_class.lower())) # Get the full span by finding I-tokens (and D- for discontinuous) # with same id ann_tokens = [B_token] for I_token in self.tokens[index:]: I_token_annotation = getattr(I_token, name_column) if I_token_annotation: I_elements = [ element.lstrip("I-").lstrip("D-") for element in I_token_annotation.split("#") if element.startswith(("I-", "D-")) ] if f"{ann_class}-{ann_id}" in I_elements: ann_tokens.append(I_token) # Create Annotation object and add to list annotation = Annotation( tokens=ann_tokens, ann_class=ann_class, ann_id=ann_id, relations=relations, ) annotations.append(annotation) return annotations
def transform_item(self, item): annotations = [] for ann in item.annotations: if ann.type == AnnotationType.bbox: if not item.has_image: raise Exception( "Image info is required for this transform") h, w = item.image.size annotations.append(self.convert_bbox(ann, h, w)) else: annotations.append(ann) return self.wrap_item(item, annotations=annotations)
def transform_item(self, item): annotations = [] for ann in item.annotations: if ann.type in { AnnotationType.mask, AnnotationType.polygon, AnnotationType.polyline, AnnotationType.points, }: annotations.append(self.convert_shape(ann)) else: annotations.append(ann) return self.wrap_item(item, annotations=annotations)
def transform_item(self, item): annotations = [ p for p in item.annotations if p.type != AnnotationType.bbox ] bboxes = [p for p in item.annotations if p.type == AnnotationType.bbox] for bbox in bboxes: annotations.append( Bbox(bbox.x - 1, bbox.y - 1, bbox.w, bbox.h, label=bbox.label, attributes=bbox.attributes)) return item.wrap(annotations=annotations)
def transform_item(self, item): annotations = [] for ann in item.annotations: if ann.type == AnnotationType.mask: polygons = self.convert_mask(ann) if not polygons: log.debug("[%s]: item %s: " "Mask conversion to polygons resulted in too " "small polygons, which were discarded" % \ (self.NAME, item.id)) annotations.extend(polygons) else: annotations.append(ann) return self.wrap_item(item, annotations=annotations)
def _get_plotly_annotations(x: list[float], y: list[float], reactions: list[Reaction]): """Returns dictionary of annotations for the Plotly figure layout""" annotations = [] for x_coord, y_coord, rxn in zip(x, y, reactions): products = ", ".join([ htmlify(p.reduced_formula) for p in rxn.products if not np.isclose(rxn.get_coeff(p), 0) ]) annotation = dict(x=x_coord, y=y_coord, text=products, font=dict(size=18), ax=-25, ay=55) annotations.append(annotation) return annotations
def transform_item(self, item): annotations = [] segments = [] for ann in item.annotations: if ann.type in {AnnotationType.polygon, AnnotationType.mask}: segments.append(ann) else: annotations.append(ann) if not segments: return item if not item.has_image: raise Exception("Image info is required for this transform") h, w = item.image.size segments = self.crop_segments(segments, w, h) annotations += segments return self.wrap_item(item, annotations=annotations)
def _parse_gcal_item_desc( gcal_item: dict) -> Tuple[List[str], str, Union[UUID, None]]: """Parse and return the necessary TW fields off a Google Calendar Item.""" annotations: List[str] = [] status = "pending" uuid = None if "description" not in gcal_item.keys(): return annotations, status, uuid gcal_desc = gcal_item["description"] # strip whitespaces, empty lines lines = [l.strip() for l in gcal_desc.split("\n") if l][1:] # annotations i = 0 for i, l in enumerate(lines): parts = l.split(":", maxsplit=1) if len(parts) == 2 and parts[0].lower().startswith("* annotation"): annotations.append(parts[1].strip()) else: break if i == len(lines) - 1: return annotations, status, uuid # Iterate through rest of lines, find only the status and uuid ones for l in lines[i:]: parts = l.split(":", maxsplit=1) if len(parts) == 2: start = parts[0].lower() if start.startswith("* status"): status = parts[1].strip().lower() elif start.startswith("* uuid"): try: uuid = UUID(parts[1].strip()) except ValueError as err: logger.error( f'Invalid UUID "{err}" provided during GCal -> TW conversion,' f" Using None...\n\n{traceback.format_exc()}") return annotations, status, uuid
def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Called when a FunctionDef node is visited.""" if not node.is_method() or node.name != "__init__": return # Check that all arguments are annotated. # The first argument is "self". args = node.args annotations = (args.posonlyargs_annotations + args.annotations + args.kwonlyargs_annotations)[1:] if args.vararg is not None: annotations.append(args.varargannotation) if args.kwarg is not None: annotations.append(args.kwargannotation) if not annotations or None in annotations: return # Check that return type is specified and it is "None". if not isinstance(node.returns, nodes.Const) or node.returns.value is not None: self.add_message("hass-constructor-return", node=node)
def transform_item(self, item): annotations = [] segments = [] for ann in item.annotations: if ann.type in {AnnotationType.polygon, AnnotationType.mask}: segments.append(ann) else: annotations.append(ann) if not segments: return item if not item.has_image: raise Exception("Image info is required for this transform") h, w = item.image.size instances = self.find_instances(segments) segments = [ self.merge_segments(i, w, h, self._include_polygons) for i in instances ] segments = sum(segments, []) annotations += segments return self.wrap_item(item, annotations=annotations)
def step_decorator(function): """ Decorator that transforms functions into `Step` and ensures that the input and output type match. """ functools.wraps(function) sig = inspect.signature(function) annotations = [] for ty in list(sig.parameters.values()): if ty.annotation is ty.empty: raise Exception( f"Missing type annotation for argument `{ty}`." " Steps require `SourceType` types for all arguments" ) annotations.append(ty.annotation) input_types: List[SourceType] = tuple(annotations) # TODO: handle tuples return types output_types = sig.return_annotation # the modified function that the decorator creates def wrapper(*args: Source) -> Source: # check to make sure the num of args match the num of expected # args if len(args) != len(input_types): raise Exception( f"Expected {len(input_types)} input arguments," f" but recieved {len(args)}" ) # make sure that the args are convertible to expected input # types for arg, inp in zip(args, input_types): assert isinstance( arg, Source ), f"Argument type is not source: ${type(arg)}" if arg.typ != inp and not arg.is_convertible_to(inp): raise Exception( f"Type mismatch: can't convert {arg.typ} to {inp}" ) # Create a source with no data so that we can return a handle # to this. # When this step executes, this is updated to contain the data # generated by the step. future_output = Source(None, output_types) # convert the args to the right types and unwrap them # NOTE(rachit): This is a *LAZY* computaion and only occurs when # the step's data has been filled. unwrapped_args = map( lambda a: a[0].convert_to(a[1]).data, zip(args, input_types) ) if builder.ctx: name = f"{'.'.join(builder.ctx)}.{function.__name__}" else: name = function.__name__ # thunk the function as a Step and add it to the current stage. builder.steps.append( Step( name, function, unwrapped_args, future_output, description, ) ) # return handle to the thing this function will return return future_output return wrapper
def plot_absorption_parameters(physical_model): """Make a simple plot showing the absorption parameters and errors.""" params = physical_model.components["absorption"].parameters param_esds = physical_model.components["absorption"].parameter_esds d = { "absorption_parameters": { "data": [{ "x": [i + 0.5 for i in range(len(params))], "y": list(params), "type": "scatter", "name": "absorption parameters", "xaxis": "x", "yaxis": "y", "mode": "markers", }], "layout": { "title": "Absorption correction surface parameters", "xaxis": { "domain": [0, 1], "anchor": "y", "title": "", "tickvals": [] }, "yaxis": { "domain": [0, 1], "anchor": "x", "title": "Parameter value" }, }, "help": absorption_help_msg, } } if param_esds: d["absorption_parameters"]["data"][-1]["error_y"] = { "type": "data", "array": list(param_esds), } light_grey = "#d3d3d3" grey = "#808080" shapes = [] lmax = int(-1 + (1 + len(params))**0.5) ls = [i + 1 for i in range(lmax)] ns = [(2 * l) + 1 for l in ls] annotations = [] start = 0 for i, n in enumerate(ns): fillcolor = [light_grey, grey][i % 2] # alternate colours shapes.append({ "type": "rect", "xref": "x", "yref": "paper", "x0": start, "y0": 0, "x1": start + n, "y1": 1, "fillcolor": fillcolor, "opacity": 0.2, "line": { "width": 0 }, }) annotations.append({ "xref": "x", "yref": "paper", "x": start + (n / 2.0), "y": 1, "text": f"l={ls[i]}", "showarrow": False, "yshift": 20, }) start += n d["absorption_parameters"]["layout"]["shapes"] = shapes d["absorption_parameters"]["layout"]["annotations"] = annotations return d
def batch_plot_shapes_and_annotations(self): light_grey = "#d3d3d3" grey = "#808080" shapes = [] annotations = [] batches = flex.int(self.batches) text = flex.std_string(batches.size()) for i, batch in enumerate(self.batch_params): fillcolor = [light_grey, grey][i % 2] # alternate colours shapes.append({ "type": "rect", # x-reference is assigned to the x-values "xref": "x", # y-reference is assigned to the plot paper [0,1] "yref": "paper", "x0": self._batch_increments[i], "y0": 0, "x1": self._batch_increments[i] + (batch["range"][1] - batch["range"][0]), "y1": 1, "fillcolor": fillcolor, "opacity": 0.2, "line": { "width": 0 }, }) annotations.append({ # x-reference is assigned to the x-values "xref": "x", # y-reference is assigned to the plot paper [0,1] "yref": "paper", "x": self._batch_increments[i] + (batch["range"][1] - batch["range"][0]) / 2, "y": 1, "text": f"{batch['id']}", "showarrow": False, "yshift": 20, # 'arrowhead': 7, # 'ax': 0, # 'ay': -40 }) sel = (batches >= batch["range"][0]) & (batches <= batch["range"][1]) text.set_selected( sel, flex.std_string([ f"{batch['id']}: {j - batch['range'][0] + 1}" for j in batches.select(sel) ]), ) return shapes, annotations, list(text)
log_format["red"] % (_type_errors_were(introduced) + " introduced ") if introduced else "" ) + f"in {f}" ) print() print(message) for introduced_type in differences: if differences[introduced_type] > 0: for error in current_errors[introduced_type]: print( f" line {error['line']} {error['error']}: {error['description']}" ) annotations.append( "%s:%s:1:%s %s" % ( str(error["filename_local"]), error["line"], error["error"], error["description"], ) ) if options.annotations: with open(options.annotations, "w") as fh: fh.write("\n".join(annotations) + "\n") if annotations: exit(1)
def parse_json_arr(data) -> list: """ parsea el array con los json para construir una instancia valida de Annotation @see #parseJson""" annotations = [] for entry in data: annotations.append(Annotation.parse_json(entry)) return annotations