def test_narrow_line_plot_with_nans_is_drawn_properly(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model display_panel = document_controller.selected_display_panel data = numpy.random.rand(200) data[0] = numpy.nan data_item = DataItem.DataItem(data) data_item.set_xdata(DataAndMetadata.new_data_and_metadata(data)) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) display_panel.display_canvas_item.layout_immediate((100, 480)) axes = display_panel.display_canvas_item._axes drawing_context = DrawingContext.DrawingContext() calibrated_data_min = axes.calibrated_data_min calibrated_data_max = axes.calibrated_data_max calibrated_data_range = calibrated_data_max - calibrated_data_min display_xdata = display_panel.display_canvas_item.line_graph_canvas_item.calibrated_xdata LineGraphCanvasItem.draw_line_graph( drawing_context, 480, 100, 0, 0, display_xdata, calibrated_data_min, calibrated_data_range, axes.calibrated_left_channel, axes.calibrated_right_channel, axes.x_calibration, "black", "black", None, axes.data_style) # ensure that the drawing commands are sufficiently populated to have drawn the graph self.assertGreater(len(drawing_context.commands), 100)
def __init__(self, ui_settings: UISettings.UISettings, delegate, event_loop, draw_background: bool = True): super().__init__() self.__ui_settings = ui_settings self.delegate = delegate self.__drawing_context_lock = threading.RLock() self.__drawing_context = DrawingContext.DrawingContext() self.__display_data = None self.__display_script = None self.__closing_lock = threading.RLock() self.__closed = False self.__data = None self.__last_data = None # canvas items get added back to front # create the child canvas items # the background self.add_canvas_item(CanvasItem.BackgroundCanvasItem()) # frame rate self.__display_frame_rate_id = None self.__display_frame_rate_last_index = 0
def test_line_plot_handle_calibrated_x_axis_with_negative_scale(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model display_panel = document_controller.selected_display_panel data = numpy.random.randn(100) data_item = DataItem.DataItem(data) data_item.set_xdata( DataAndMetadata.new_data_and_metadata( data, dimensional_calibrations=[ Calibration.Calibration(0, -1.0, "e") ])) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) display_panel.display_canvas_item.layout_immediate((640, 480)) axes = display_panel.display_canvas_item._axes drawing_context = DrawingContext.DrawingContext() calibrated_data_min = axes.calibrated_data_min calibrated_data_max = axes.calibrated_data_max calibrated_data_range = calibrated_data_max - calibrated_data_min LineGraphCanvasItem.draw_line_graph( drawing_context, 480, 640, 0, 0, data_item.xdata, calibrated_data_min, calibrated_data_range, axes.calibrated_left_channel, axes.calibrated_right_channel, axes.x_calibration, "black", "black", None, axes.data_style) # ensure that the drawing commands are sufficiently populated to have drawn the graph self.assertGreater(len(drawing_context.commands), 100)
def __init__( self, ui_settings: UISettings.UISettings, delegate: typing.Optional[DisplayCanvasItem.DisplayCanvasItemDelegate] ) -> None: super().__init__() self.__ui_settings = ui_settings self.delegate = delegate self.__drawing_context_lock = threading.RLock() self.__drawing_context = DrawingContext.DrawingContext() self.__display_xdata: typing.Optional[ DataAndMetadata.DataAndMetadata] = None self.__display_script: typing.Optional[str] = None self.__closing_lock = threading.RLock() self.__closed = False # canvas items get added back to front # create the child canvas items # the background self.add_canvas_item(CanvasItem.BackgroundCanvasItem()) # frame rate self.__display_frame_rate_id: typing.Optional[str] = None self.__display_frame_rate_last_index = 0
def test_draw_data_with_color_table(self): dc = DrawingContext.DrawingContext() data = numpy.zeros((4, 4), numpy.float32) color_map_data = numpy.zeros((256, ), numpy.uint32) color_map_data[:] = 0xFF010203 dc.draw_data(data, 0, 0, 4, 4, 0, 1, color_map_data) dc.to_svg(Geometry.IntSize(4, 4), Geometry.IntRect.from_tlbr(0, 0, 4, 4))
def __create_thumbnail(self, draw_rect: Geometry.IntRect) -> DrawingContext.DrawingContext: drawing_context = DrawingContext.DrawingContext() if self.__display_item: thumbnail_data = self.calculate_thumbnail_data() if thumbnail_data is not None: draw_rect = Geometry.fit_to_size(draw_rect, thumbnail_data.shape) drawing_context.draw_image(thumbnail_data, draw_rect[0][1], draw_rect[0][0], draw_rect[1][1], draw_rect[1][0]) return drawing_context
def get_calculated_data(self, ui): drawing_context, shape = DisplayPanel.preview(ui.get_font_metrics, self.__display_item, 512, 512) thumbnail_drawing_context = DrawingContext.DrawingContext() thumbnail_drawing_context.scale(self.width / 512, self.height / 512) thumbnail_drawing_context.translate(0, (shape[1] - shape[0]) * 0.5) thumbnail_drawing_context.add(drawing_context) return ui.create_rgba_image(thumbnail_drawing_context, self.width, self.height)
def __get_calculated_data( self, ui: UserInterface.UserInterface ) -> typing.Optional[DrawingContext.RGBA32Type]: drawing_context, shape = DisplayPanel.preview( DisplayPanel.DisplayPanelUISettings(ui), self.__display_item, 512, 512) thumbnail_drawing_context = DrawingContext.DrawingContext() thumbnail_drawing_context.scale(self.width / 512, self.height / 512) thumbnail_drawing_context.translate(0, (shape[1] - shape[0]) * 0.5) thumbnail_drawing_context.add(drawing_context) return ui.create_rgba_image(thumbnail_drawing_context, self.width, self.height)
def __create_thumbnail( self, draw_rect: Geometry.IntRect) -> DrawingContext.DrawingContext: drawing_context = DrawingContext.DrawingContext() if self.__display_item: thumbnail_data = self.calculate_thumbnail_data() if thumbnail_data is not None: draw_rect_f = Geometry.fit_to_size( draw_rect, typing.cast(typing.Tuple[int, int], thumbnail_data.shape)) drawing_context.draw_image(thumbnail_data, draw_rect_f.left, draw_rect_f.top, draw_rect_f.width, draw_rect_f.height) return drawing_context
def test_1d_data_displayed_as_2d(self): # setup with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model display_panel = document_controller.selected_display_panel data_item = DataItem.DataItem(numpy.zeros((10, ))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) display_item.display_type = "image" header_height = display_panel.header_canvas_item.header_height display_panel.root_container.layout_immediate( (1000 + header_height, 1000)) drawing_context = DrawingContext.DrawingContext() display_panel.root_container.repaint_immediate( drawing_context, display_panel.root_container.canvas_size)
def paint_cell(self, drawing_context: DrawingContext.DrawingContext, rect: Geometry.IntRect, style: typing.Set[str]) -> None: # style: "disabled" (default is enabled) margin_rect = rect.inset(4, 4) drawing_context.begin_path() drawing_context.rect(margin_rect.left, margin_rect.top, margin_rect.width, margin_rect.height) drawing_context.fill_style = "#BBB" drawing_context.fill() inset_rect = margin_rect.inset(4, 4) drawing_context.begin_path() drawing_context.rect(inset_rect.left, inset_rect.top, inset_rect.width, inset_rect.height) drawing_context.close_path() drawing_context.fill_style = self.__color drawing_context.fill() drawing_context.begin_path() drawing_context.move_to(inset_rect.right, inset_rect.top) drawing_context.line_to(inset_rect.right, inset_rect.bottom) drawing_context.line_to(inset_rect.left, inset_rect.bottom) drawing_context.close_path() drawing_context.fill_style = DrawingContext.color_without_alpha( self.__color) drawing_context.fill() drawing_context.begin_path() drawing_context.rect(inset_rect.left, inset_rect.top, inset_rect.width, inset_rect.height) drawing_context.stroke_style = "#454545" drawing_context.stroke() if "disabled" in style: drawing_context.begin_path() drawing_context.rect(margin_rect.left, margin_rect.top, margin_rect.width, margin_rect.height) drawing_context.fill_style = "rgba(255, 255, 255, 0.5)" drawing_context.fill()
def test_line_plot_handles_data_below_one_in_log_scale(self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController(self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): display_panel = document_controller.selected_display_panel data = numpy.random.rand(100) data_item = DataItem.DataItem(data) data_item.set_xdata(DataAndMetadata.new_data_and_metadata(data)) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item(data_item) display_item.set_display_property("y_style", "log") display_panel.set_display_panel_display_item(display_item) display_panel.display_canvas_item.layout_immediate((640, 480)) axes = display_panel.display_canvas_item._axes self.assertEqual(axes.data_style, "log") drawing_context = DrawingContext.DrawingContext() calibrated_data_min = axes.calibrated_data_min calibrated_data_max = axes.calibrated_data_max calibrated_data_range = calibrated_data_max - calibrated_data_min display_xdata = display_panel.display_canvas_item.line_graph_canvas_item.calibrated_xdata LineGraphCanvasItem.draw_line_graph(drawing_context, 480, 640, 0, 0, display_xdata, calibrated_data_min, calibrated_data_range, axes.calibrated_left_channel, axes.calibrated_right_channel, axes.x_calibration, "black", "black", None, axes.data_style) # ensure that the drawing commands are sufficiently populated to have drawn the graph self.assertGreater(len(drawing_context.commands), 100)
def _prepare_render(self) -> None: data_and_metadata = self.__display_xdata display_script = self.__display_script if data_and_metadata and display_script: # this method may trigger a layout of its parent scroll area. however, the parent scroll # area may already be closed. this is a stop-gap guess at a solution - the basic idea being # that this object is not closeable while this method is running; and this method should not # run if the object is already closed. with self.__closing_lock: if self.__closed: return assert not self.__closed # Update the display state. rect = self.canvas_bounds if rect is not None: g: typing.Dict[str, typing.Any] = dict() drawing_context = DrawingContext.DrawingContext() g["drawing_context"] = drawing_context g["display_data_and_metadata"] = data_and_metadata g["bounds"] = rect g["get_font_metrics_fn"] = self.__ui_settings.get_font_metrics l: typing.Dict[str, typing.Any] = dict() try: # print(code) compiled = compile(display_script, "expr", "exec") exec(compiled, g, l) except Exception as e: # import sys, traceback # traceback.print_exc() # traceback.format_exception(*sys.exc_info()) print( str(e) or "Unable to evaluate display script." ) # a stack trace would be too much information right now with self.__drawing_context_lock: self.__drawing_context = drawing_context
def __init__(self, figure, get_font_metrics_fn): super().__init__(figure) self.__drawing_context = DrawingContext.DrawingContext() self.__get_font_metrics_fn = get_font_metrics_fn