Skip to content

Commit d73e857

Browse files
committed
format code, update notebook with mapper widget example
1 parent 1083e95 commit d73e857

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

examples/example_mapper_anywidget.ipynb

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"id": "0",
66
"metadata": {},
77
"source": [
8-
"# Mapper interactive widget"
8+
"# Coordinates Mapper interactive widget"
99
]
1010
},
1111
{
@@ -30,7 +30,7 @@
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
33-
"from scanplot.view import MapperCanvasWidget\n",
33+
"from scanplot.view import MapperCanvasWidget, CartesianCoordsMapperWidget, CoordinatesMapperWidget\n",
3434
"from scanplot.io import load_image"
3535
]
3636
},
@@ -44,21 +44,29 @@
4444
"plot_number = 22\n",
4545
"img_path = f\"../datasets/plot_images/plot{plot_number}.png\"\n",
4646
"\n",
47-
"image = load_image(img_path)\n"
47+
"image = load_image(img_path)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"id": "4",
53+
"metadata": {},
54+
"source": [
55+
"## Mapper Canvas Widget using anywidget"
4856
]
4957
},
5058
{
5159
"cell_type": "code",
5260
"execution_count": null,
53-
"id": "4",
61+
"id": "5",
5462
"metadata": {},
5563
"outputs": [],
5664
"source": [
57-
"canvas_widget = MapperCanvasWidget()\n",
65+
"mapper_widget_canvas = MapperCanvasWidget()\n",
5866
"\n",
59-
"canvas_widget.set_image(image)\n",
67+
"mapper_widget_canvas.set_image(image)\n",
6068
"\n",
61-
"canvas_widget.apply_widget_settings(\n",
69+
"mapper_widget_canvas.apply_widget_settings(\n",
6270
" fig_size=8,\n",
6371
" line_width=1,\n",
6472
" line_color=\"red\",\n",
@@ -70,17 +78,64 @@
7078
{
7179
"cell_type": "code",
7280
"execution_count": null,
73-
"id": "5",
81+
"id": "6",
7482
"metadata": {},
7583
"outputs": [],
7684
"source": [
77-
"canvas_widget"
85+
"mapper_widget_canvas"
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"id": "7",
91+
"metadata": {},
92+
"source": [
93+
"## Mapper Full Widget"
7894
]
7995
},
8096
{
8197
"cell_type": "code",
8298
"execution_count": null,
83-
"id": "6",
99+
"id": "8",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": [
103+
"mapper_widget = CartesianCoordsMapperWidget(image)\n",
104+
"\n",
105+
"mapper_widget.apply_widget_settings(\n",
106+
" fig_size=8,\n",
107+
" line_width=1,\n",
108+
" line_color=\"red\",\n",
109+
" marker_size=6,\n",
110+
" marker_color=\"green\"\n",
111+
")"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"id": "9",
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"mapper_widget.widget()"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"id": "10",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"params = mapper_widget.export_parameters()\n",
132+
"params"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": null,
138+
"id": "11",
84139
"metadata": {},
85140
"outputs": [],
86141
"source": []

src/scanplot/view/coords_mapper_widget.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import numpy as np
33
from ipywidgets import HBox, VBox, fixed
44

5-
from .mapper_js_widget import MapperCanvasWidget
6-
75
from scanplot.plotting import draw_axes_mapping_lines
86
from scanplot.types import ConverterParameters, ImageLike
97

8+
from .mapper_js_widget import MapperCanvasWidget
9+
1010

1111
class CartesianCoordsMapperWidget:
1212
def __init__(self, plot_image: ImageLike):
@@ -22,29 +22,27 @@ def __init__(self, plot_image: ImageLike):
2222
self._x_axis_type_dropdown = self._get_x_axis_type_dropdown()
2323
self._y_axis_type_dropdown = self._get_y_axis_type_dropdown()
2424

25-
2625
@property
2726
def _x1_pixel_position(self):
2827
return round(self._canvas_mapper_widget.vline_left)
29-
28+
3029
@property
3130
def _x2_pixel_position(self):
3231
return round(self._canvas_mapper_widget.vline_right)
33-
32+
3433
@property
3534
def _y1_pixel_position(self):
3635
return round(self._canvas_mapper_widget.hline_lower)
37-
36+
3837
@property
3938
def _y2_pixel_position(self):
4039
return round(self._canvas_mapper_widget.hline_upper)
41-
40+
4241
@property
4342
def _is_valid(self) -> bool:
4443
return (self._x1_widget.value != self._x2_widget.value) and \
4544
(self._y1_widget.value != self._y2_widget.value) # fmt: skip
4645

47-
4846
def apply_widget_settings(
4947
self,
5048
fig_size: float = 8,
@@ -80,7 +78,7 @@ def widget(self) -> ipywidgets.widgets.widget_box:
8078
return box_final
8179

8280
def export_parameters(self) -> ConverterParameters:
83-
81+
8482
params = ConverterParameters(
8583
x_min_px=self._x1_pixel_position,
8684
x_max_px=self._x2_pixel_position,
@@ -95,12 +93,10 @@ def export_parameters(self) -> ConverterParameters:
9593
)
9694
return params
9795

98-
9996
def _init_canvas_mapper_widget(self):
10097
canvas_widget = MapperCanvasWidget()
10198
canvas_widget.set_image(self._image)
10299
return canvas_widget
103-
104100

105101
@staticmethod
106102
def _get_x1_widget():

0 commit comments

Comments
 (0)