|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
| 14 | +from __future__ import annotations |
14 | 15 |
|
15 | 16 | import base64 |
16 | 17 | import io |
17 | | -from typing import Any, List, Mapping, Optional, Sequence |
| 18 | +from collections.abc import Mapping, Sequence |
| 19 | +from typing import Any |
18 | 20 |
|
19 | 21 | import numpy as np |
20 | 22 | import PIL |
21 | 23 | import umap |
| 24 | +from bokeh import __version__ |
22 | 25 | from bokeh.plotting import ColumnDataSource, figure, output_notebook, show |
23 | 26 | from distinctipy import distinctipy |
24 | 27 | from tqdm.auto import tqdm |
25 | 28 |
|
26 | 29 | from tensorflow_similarity.types import FloatTensor, Tensor |
27 | 30 |
|
28 | 31 |
|
29 | | -def tensor2images(tensor: Tensor, size: Optional[int] = 64) -> List[str]: |
| 32 | +def tensor2images(tensor: Tensor, size: int = 64) -> list[str]: |
30 | 33 | """Convert tensor images back to in memory images |
31 | 34 | encoded in base 64. |
32 | 35 |
|
@@ -69,11 +72,11 @@ def tensor2images(tensor: Tensor, size: Optional[int] = 64) -> List[str]: |
69 | 72 |
|
70 | 73 | def projector( |
71 | 74 | embeddings: FloatTensor, |
72 | | - labels: Optional[Sequence[Any]] = None, |
73 | | - class_mapping: Optional[Sequence[int]] = None, |
74 | | - images: Optional[Tensor] = None, |
| 75 | + labels: Sequence[Any] | None = None, |
| 76 | + class_mapping: Sequence[int] | None = None, |
| 77 | + images: Tensor | None = None, |
75 | 78 | image_size: int = 64, |
76 | | - tooltips_info: Optional[Mapping[str, Sequence[str]]] = None, |
| 79 | + tooltips_info: Mapping[str, Sequence[str]] | None = None, |
77 | 80 | pt_size: int = 3, |
78 | 81 | colorize: bool = True, |
79 | 82 | pastel_factor: float = 0.1, |
@@ -183,13 +186,23 @@ def projector( |
183 | 186 | # to bokeh format |
184 | 187 | source = ColumnDataSource(data=data) |
185 | 188 | output_notebook() |
186 | | - fig = figure( |
187 | | - tooltips=tooltips, |
188 | | - plot_width=plot_size, |
189 | | - plot_height=plot_size, |
190 | | - active_drag=active_drag, |
191 | | - active_scroll="wheel_zoom", |
192 | | - ) |
| 189 | + # Bokeh backward compatibility |
| 190 | + if int(__version__.split(".")[0]) >= 3: |
| 191 | + fig = figure( |
| 192 | + tooltips=tooltips, |
| 193 | + width=plot_size, |
| 194 | + height=plot_size, |
| 195 | + active_drag=active_drag, |
| 196 | + active_scroll="wheel_zoom", |
| 197 | + ) |
| 198 | + else: |
| 199 | + fig = figure( |
| 200 | + tooltips=tooltips, |
| 201 | + plot_width=plot_size, |
| 202 | + plot_height=plot_size, |
| 203 | + active_drag=active_drag, |
| 204 | + active_scroll="wheel_zoom", |
| 205 | + ) |
193 | 206 |
|
194 | 207 | # remove grid and axis |
195 | 208 | fig.xaxis.visible = False |
|
0 commit comments