Skip to content

Commit 8b2d46f

Browse files
authored
Merge pull request #232 from NeuroML/experimental
To v1.0.8; Catches import of vispy so won't fail if not installed.
2 parents 0f99a51 + 446ff42 commit 8b2d46f

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

pyneuroml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
__version__ = "1.0.7"
3+
__version__ = "1.0.8"
44

55
JNEUROML_VERSION = "0.12.2"
66

pyneuroml/utils/plot.py

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,73 @@
1717
from matplotlib.lines import Line2D
1818
from matplotlib.patches import Rectangle
1919
from matplotlib_scalebar.scalebar import ScaleBar
20-
from vispy import scene
21-
from vispy.scene import SceneCanvas
22-
from vispy.app import Timer
20+
2321
from neuroml import Cell, Segment
2422

2523

2624
logger = logging.getLogger(__name__)
2725
logger.setLevel(logging.INFO)
2826

29-
3027
VISPY_THEME = {
3128
"light": {"bg": "white", "fg": "black"},
3229
"dark": {"bg": "black", "fg": "white"},
3330
}
3431
PYNEUROML_VISPY_THEME = "light"
3532

33+
try:
34+
from vispy import scene
35+
from vispy.scene import SceneCanvas
36+
from vispy.app import Timer
37+
38+
39+
def add_text_to_vispy_3D_plot(
40+
current_scene: SceneCanvas,
41+
xv: typing.List[float],
42+
yv: typing.List[float],
43+
zv: typing.List[float],
44+
color: str,
45+
text: str,
46+
):
47+
"""Add text to a vispy plot between two points.
48+
49+
Wrapper around vispy.scene.visuals.Text
50+
51+
Rotates the text label to ensure it is at the same angle as the line.
52+
53+
:param scene: vispy scene object
54+
:type scene: SceneCanvas
55+
:param xv: start and end coordinates in one axis
56+
:type xv: list[x1, x2]
57+
:param yv: start and end coordinates in second axis
58+
:type yv: list[y1, y2]
59+
:param zv: start and end coordinates in third axix
60+
:type zv: list[z1, z2]
61+
:param color: color of text
62+
:type color: str
63+
:param text: text to write
64+
:type text: str
65+
"""
66+
67+
angle = int(numpy.rad2deg(numpy.arctan2((yv[1] - yv[0]), (xv[1] - xv[0]))))
68+
if angle > 90:
69+
angle -= 180
70+
elif angle < -90:
71+
angle += 180
72+
73+
return scene.Text(
74+
pos=((xv[0] + xv[1]) / 2, (yv[0] + yv[1]) / 2, (zv[0] + zv[1]) / 2),
75+
text=text,
76+
color=color,
77+
rotation=angle,
78+
parent=current_scene,
79+
)
3680

37-
def add_text_to_vispy_3D_plot(
38-
current_scene: SceneCanvas,
39-
xv: typing.List[float],
40-
yv: typing.List[float],
41-
zv: typing.List[float],
42-
color: str,
43-
text: str,
44-
):
45-
"""Add text to a vispy plot between two points.
46-
47-
Wrapper around vispy.scene.visuals.Text
81+
except:
82+
print("\n**************************\n Please install vispy to use 3D plotting features!\n**************************")
83+
4884

49-
Rotates the text label to ensure it is at the same angle as the line.
5085

51-
:param scene: vispy scene object
52-
:type scene: SceneCanvas
53-
:param xv: start and end coordinates in one axis
54-
:type xv: list[x1, x2]
55-
:param yv: start and end coordinates in second axis
56-
:type yv: list[y1, y2]
57-
:param zv: start and end coordinates in third axix
58-
:type zv: list[z1, z2]
59-
:param color: color of text
60-
:type color: str
61-
:param text: text to write
62-
:type text: str
63-
"""
64-
angle = int(numpy.rad2deg(numpy.arctan2((yv[1] - yv[0]), (xv[1] - xv[0]))))
65-
if angle > 90:
66-
angle -= 180
67-
elif angle < -90:
68-
angle += 180
6986

70-
return scene.Text(
71-
pos=((xv[0] + xv[1]) / 2, (yv[0] + yv[1]) / 2, (zv[0] + zv[1]) / 2),
72-
text=text,
73-
color=color,
74-
rotation=angle,
75-
parent=current_scene,
76-
)
7787

7888

7989
def add_text_to_matplotlib_2D_plot(

0 commit comments

Comments
 (0)