ephys-link 2.0.0__py3-none-any.whl → 2.0.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,176 +1,133 @@
1
- """Binding methods for Ephys Link manipulator platforms.
2
-
3
- Definition of the methods a platform binding class must implement to be used by Ephys Link.
4
-
5
- Usage:
6
- Implement the BaseBindings class when defining a platform binding to ensure it supports the necessary methods.
7
- """
8
-
9
- from abc import ABC, abstractmethod
10
-
11
- from vbl_aquarium.models.unity import Vector3, Vector4
12
-
13
-
14
- class BaseBinding(ABC):
15
- """Base class to enforce bindings manipulator platforms will support.
16
-
17
- No need to catch exceptions as the [Platform Handler][ephys_link.back_end.platform_handler] will catch them.
18
- """
19
-
20
- @staticmethod
21
- @abstractmethod
22
- def get_display_name() -> str:
23
- """Get the full display name of the platform.
24
-
25
- Returns:
26
- Full display name of the platform.
27
- """
28
-
29
- @staticmethod
30
- @abstractmethod
31
- def get_cli_name() -> str:
32
- """Get the name of the platform for CLI usage.
33
-
34
- This is the value used to identify the platform when using the `-t` flag in the CLI.
35
-
36
- Returns:
37
- Name of the platform to use on the CLI.
38
- """
39
-
40
- @abstractmethod
41
- async def get_axes_count(self) -> int:
42
- """Get the number of axes for the current platform.
43
-
44
- Returns:
45
- Number of axes.
46
- """
47
-
48
- @abstractmethod
49
- def get_dimensions(self) -> Vector4:
50
- """Get the dimensions of the manipulators on the current platform (mm).
51
-
52
- For 3-axis manipulators, copy the dimension of the axis parallel to the probe into w.
53
-
54
- Returns:
55
- Dimensions of the manipulators.
56
- """
57
-
58
- @abstractmethod
59
- async def get_manipulators(self) -> list[str]:
60
- """Get a list of available manipulators on the current platform.
61
-
62
- Returns:
63
- List of manipulator IDs.
64
- """
65
-
66
- @abstractmethod
67
- async def get_position(self, manipulator_id: str) -> Vector4:
68
- """Get the current position of a manipulator.
69
-
70
- These will be the translation values of the manipulator (mm), so they may need to be rotated to unified space.
71
- For 3-axis manipulators, copy the position of the axis parallel to the probe into w.
72
-
73
- Args:
74
- manipulator_id: Manipulator ID.
75
-
76
- Returns:
77
- Current position of the manipulator in platform space (mm).
78
- """
79
-
80
- @abstractmethod
81
- async def get_angles(self, manipulator_id: str) -> Vector3:
82
- """Get the current rotation angles of a manipulator in Yaw, Pitch, Roll (degrees).
83
-
84
- Args:
85
- manipulator_id: Manipulator ID.
86
-
87
- Returns:
88
- Current angles of the manipulator.
89
- """
90
-
91
- @abstractmethod
92
- async def get_shank_count(self, manipulator_id: str) -> int:
93
- """Get the number of shanks on a manipulator.
94
-
95
- Args:
96
- manipulator_id: Manipulator ID.
97
-
98
- Returns:
99
- Number of shanks on the manipulator.
100
- """
101
-
102
- @abstractmethod
103
- def get_movement_tolerance(self) -> float:
104
- """Get the tolerance for how close the final position must be to the target position in a movement (mm).
105
-
106
- Returns:
107
- Movement tolerance (mm).
108
- """
109
-
110
- @abstractmethod
111
- async def set_position(self, manipulator_id: str, position: Vector4, speed: float) -> Vector4:
112
- """Set the position of a manipulator.
113
-
114
- This will directly set the position in the original platform space.
115
- For 3-axis manipulators, the first 3 values of the position will be used.
116
-
117
- Args:
118
- manipulator_id: Manipulator ID.
119
- position: Platform space position to set the manipulator to (mm).
120
- speed: Speed to move the manipulator to the position (mm/s).
121
-
122
- Returns:
123
- Final position of the manipulator in platform space (mm).
124
- """
125
-
126
- @abstractmethod
127
- async def set_depth(self, manipulator_id: str, depth: float, speed: float) -> float:
128
- """Set the depth of a manipulator.
129
-
130
- This will directly set the depth stage in the original platform space.
131
-
132
- Args:
133
- manipulator_id: Manipulator ID.
134
- depth: Depth to set the manipulator to (mm).
135
- speed: Speed to move the manipulator to the depth (mm/s).
136
-
137
- Returns:
138
- Final depth of the manipulator in platform space (mm).
139
- """
140
-
141
- @abstractmethod
142
- async def stop(self, manipulator_id: str) -> None:
143
- """Stop a manipulator.
144
-
145
- Args:
146
- manipulator_id: Manipulator ID.
147
- """
148
-
149
- @abstractmethod
150
- def platform_space_to_unified_space(self, platform_space: Vector4) -> Vector4:
151
- """Convert platform space coordinates to unified space coordinates.
152
-
153
- This is an axes-swapping transformation.
154
-
155
- Unified coordinate space is the standard left-handed cartesian coordinate system
156
- with an additional depth axis pointing from the base of the probe to the tip.
157
-
158
- Args:
159
- platform_space: Platform space coordinates.
160
-
161
- Returns:
162
- Unified space coordinates.
163
- """
164
-
165
- @abstractmethod
166
- def unified_space_to_platform_space(self, unified_space: Vector4) -> Vector4:
167
- """Convert unified space coordinates to platform space coordinates.
168
-
169
- This is an axes-swapping transformation.
170
-
171
- Args:
172
- unified_space: Unified space coordinates.
173
-
174
- Returns:
175
- Platform space coordinates.
176
- """
1
+ """Binding methods for Ephys Link manipulator platforms.
2
+
3
+ Definition of the methods a platform binding class must implement to be used by Ephys Link.
4
+
5
+ Usage: Implement the BaseBindings class when defining a platform binding to ensure it supports the necessary methods.
6
+ """
7
+
8
+ from abc import ABC, abstractmethod
9
+
10
+ from vbl_aquarium.models.unity import Vector3, Vector4
11
+
12
+
13
+ class BaseBindings(ABC):
14
+ """Base class to enforce bindings manipulator platforms will support.
15
+
16
+ No need to catch exceptions as the Platform Handler will catch them.
17
+ """
18
+
19
+ @abstractmethod
20
+ async def get_manipulators(self) -> list[str]:
21
+ """Get a list of available manipulators on the current platform.
22
+
23
+ :returns: List of manipulator IDs.
24
+ :rtype: list[str]
25
+ """
26
+
27
+ @abstractmethod
28
+ async def get_num_axes(self) -> int:
29
+ """Get the number of axes for the current platform.
30
+
31
+ :returns: Number of axes.
32
+ :rtype: int
33
+ """
34
+
35
+ @abstractmethod
36
+ def get_dimensions(self) -> Vector4:
37
+ """Get the dimensions of the manipulators on the current platform (mm).
38
+
39
+ For 3-axis manipulators, copy the dimension of the axis parallel to the probe into w.
40
+
41
+ :returns: Dimensions of the manipulators.
42
+ :rtype: Vector4
43
+ """
44
+
45
+ @abstractmethod
46
+ async def get_position(self, manipulator_id: str) -> Vector4:
47
+ """Get the current position of a manipulator.
48
+
49
+ These will be the translation values of the manipulator (mm), so they may need to be rotated to unified space.
50
+ For 3-axis manipulators, copy the position of the axis parallel to the probe into w.
51
+
52
+ :param manipulator_id: Manipulator ID.
53
+ :type manipulator_id: str
54
+ :returns: Current position of the manipulator in platform space (mm).
55
+ :rtype: Vector4
56
+ """
57
+
58
+ @abstractmethod
59
+ async def get_angles(self, manipulator_id: str) -> Vector3:
60
+ """Get the current rotation angles of a manipulator in Yaw, Pitch, Roll (degrees).
61
+
62
+ :param manipulator_id: Manipulator ID.
63
+ :type manipulator_id: str
64
+ :returns: Current angles of the manipulator.
65
+ :rtype: Vector3
66
+ """
67
+
68
+ @abstractmethod
69
+ async def get_shank_count(self, manipulator_id: str) -> int:
70
+ """Get the number of shanks on a manipulator.
71
+
72
+ :param manipulator_id: Manipulator ID.
73
+ :type manipulator_id: str
74
+ :returns: Number of shanks on the manipulator.
75
+ :rtype: int
76
+ """
77
+
78
+ @abstractmethod
79
+ async def get_movement_tolerance(self) -> float:
80
+ """Get the tolerance for how close the final position must be to the target position in a movement (mm).
81
+
82
+ :returns: Movement tolerance (mm).
83
+ :rtype: float
84
+ """
85
+
86
+ @abstractmethod
87
+ async def set_position(self, manipulator_id: str, position: Vector4, speed: float) -> Vector4:
88
+ """Set the position of a manipulator.
89
+
90
+ This will directly set the position in the original platform space.
91
+ Unified space coordinates will need to be converted to platform space.
92
+ For 3-axis manipulators, the first 3 values of the position will be used.
93
+
94
+ :param manipulator_id: Manipulator ID.
95
+ :type manipulator_id: str
96
+ :param position: Platform space position to set the manipulator to (mm).
97
+ :type position: Vector4
98
+ :param speed: Speed to move the manipulator to the position (mm/s).
99
+ :type speed: float
100
+ :returns: Final position of the manipulator in platform space (mm).
101
+ :rtype: Vector4
102
+ """
103
+
104
+ @abstractmethod
105
+ async def stop(self, manipulator_id: str) -> None:
106
+ """Stop a manipulator."""
107
+
108
+ @abstractmethod
109
+ def platform_space_to_unified_space(self, platform_space: Vector4) -> Vector4:
110
+ """Convert platform space coordinates to unified space coordinates.
111
+
112
+ This is an axes-swapping transformation.
113
+
114
+ Unified coordinate space is the standard left-handed cartesian coordinate system
115
+ with an additional depth axis pointing from the base of the probe to the tip.
116
+
117
+ :param platform_space: Platform space coordinates.
118
+ :type platform_space: Vector4
119
+ :returns: Unified space coordinates.
120
+ :rtype: Vector4
121
+ """
122
+
123
+ @abstractmethod
124
+ def unified_space_to_platform_space(self, unified_space: Vector4) -> Vector4:
125
+ """Convert unified space coordinates to platform space coordinates.
126
+
127
+ This is an axes-swapping transformation.
128
+
129
+ :param unified_space: Unified space coordinates.
130
+ :type unified_space: Vector4
131
+ :returns: Platform space coordinates.
132
+ :rtype: Vector4
133
+ """
@@ -0,0 +1,121 @@
1
+ # ruff: noqa: T201
2
+ """Commonly used utility functions and constants."""
3
+
4
+ from os.path import join
5
+ from pathlib import Path
6
+
7
+ from packaging.version import parse
8
+ from requests import get
9
+ from vbl_aquarium.models.unity import Vector4
10
+
11
+ from ephys_link.__about__ import __version__
12
+ from ephys_link.util.console import Console
13
+
14
+ # Ephys Link ASCII.
15
+ ASCII = r"""
16
+ ______ _ _ _ _
17
+ | ____| | | | | (_) | |
18
+ | |__ _ __ | |__ _ _ ___ | | _ _ __ | | __
19
+ | __| | '_ \| '_ \| | | / __| | | | | '_ \| |/ /
20
+ | |____| |_) | | | | |_| \__ \ | |____| | | | | <
21
+ |______| .__/|_| |_|\__, |___/ |______|_|_| |_|_|\_\
22
+ | | __/ |
23
+ |_| |___/
24
+ """
25
+
26
+ # Absolute path to the resource folder.
27
+ RESOURCES_PATH = join(str(Path(__file__).parent.parent.absolute()), "resources")
28
+
29
+ # Ephys Link Port
30
+ PORT = 3000
31
+
32
+
33
+ # Server startup.
34
+ def server_preamble() -> None:
35
+ """Print the server startup preamble."""
36
+ print(ASCII)
37
+ print(__version__)
38
+ print()
39
+ print("This is the Ephys Link server window.")
40
+ print("You may safely leave it running in the background.")
41
+ print("To stop it, close this window or press CTRL + Pause/Break.")
42
+ print()
43
+
44
+
45
+ def check_for_updates() -> None:
46
+ """Check for updates to the Ephys Link."""
47
+ response = get("https://api.github.com/repos/VirtualBrainLab/ephys-link/tags", timeout=10)
48
+ latest_version = response.json()[0]["name"]
49
+ if parse(latest_version) > parse(__version__):
50
+ Console.info_print("Update available", latest_version)
51
+ Console.info_print("", "Download at: https://github.com/VirtualBrainLab/ephys-link/releases/latest")
52
+
53
+
54
+ # Unit conversions
55
+
56
+
57
+ def mmps_to_umps(mmps: float) -> float:
58
+ """Convert millimeters per second to micrometers per second.
59
+
60
+ :param mmps: Speed in millimeters per second.
61
+ :type mmps: float
62
+ :returns: Speed in micrometers per second.
63
+ :rtype: float
64
+ """
65
+ return mmps * 1_000
66
+
67
+
68
+ def mm_to_um(mm: Vector4) -> Vector4:
69
+ """Convert millimeters to micrometers.
70
+
71
+ :param mm: Length in millimeters.
72
+ :type mm: Vector4
73
+ :returns: Length in micrometers.
74
+ :rtype: Vector4
75
+ """
76
+ return mm * 1_000
77
+
78
+
79
+ def um_to_mm(um: Vector4) -> Vector4:
80
+ """Convert micrometers to millimeters.
81
+
82
+ :param um: Length in micrometers.
83
+ :type um: Vector4
84
+ :returns: Length in millimeters.
85
+ :rtype: Vector4
86
+ """
87
+ return um / 1_000
88
+
89
+
90
+ def vector4_to_array(vector4: Vector4) -> list[float]:
91
+ """Convert a Vector4 to a list of floats.
92
+
93
+ :param vector4: Vector4 to convert.
94
+ :type vector4: :class:`vbl_aquarium.models.unity.Vector4`
95
+ :return: List of floats.
96
+ :rtype: list[float]
97
+ """
98
+ return [vector4.x, vector4.y, vector4.z, vector4.w]
99
+
100
+
101
+ def array_to_vector4(array: list[float]) -> Vector4:
102
+ """Convert a list of floats to a Vector4.
103
+
104
+ :param array: List of floats.
105
+ :type array: list[float]
106
+ :return: First four elements of the list as a Vector4 padded with zeros if necessary.
107
+ :rtype: :class:`vbl_aquarium.models.unity.Vector4`
108
+ """
109
+
110
+ def get_element(this_array: list[float], index: int) -> float:
111
+ try:
112
+ return this_array[index]
113
+ except IndexError:
114
+ return 0.0
115
+
116
+ return Vector4(
117
+ x=get_element(array, 0),
118
+ y=get_element(array, 1),
119
+ z=get_element(array, 2),
120
+ w=get_element(array, 3),
121
+ )
@@ -0,0 +1,112 @@
1
+ # ruff: noqa: T201
2
+ """Console class for printing messages to the console.
3
+
4
+ Configure the console to print error and debug messages.
5
+
6
+ Usage: Create a Console object and call the appropriate method to print messages.
7
+ """
8
+
9
+ from traceback import print_exc
10
+
11
+ from colorama import Back, Fore, Style, init
12
+
13
+ # Constants.
14
+ TAB_BLOCK = "\t\t"
15
+
16
+
17
+ class Console:
18
+ def __init__(self, *, enable_debug: bool) -> None:
19
+ """Initialize console properties.
20
+
21
+ :param enable_debug: Enable debug mode.
22
+ :type enable_debug: bool
23
+ """
24
+ self._enable_debug = enable_debug
25
+
26
+ # Repeat message fields.
27
+ self._last_message = ""
28
+ self._repeat_counter = 1
29
+
30
+ # Initialize colorama.
31
+ init(autoreset=True)
32
+
33
+ @staticmethod
34
+ def error_print(msg: str) -> None:
35
+ """Print an error message to the console.
36
+
37
+ :param msg: Error message to print.
38
+ :type msg: str
39
+ """
40
+ print(f"\n{Back.RED}{Style.BRIGHT} ERROR {Style.RESET_ALL}{TAB_BLOCK}{Fore.RED}{msg}")
41
+
42
+ @staticmethod
43
+ def labeled_error_print(label: str, msg: str) -> None:
44
+ """Print an error message with a label to the console.
45
+
46
+ :param label: Label for the error message.
47
+ :type label: str
48
+ :param msg: Error message to print.
49
+ :type msg: str
50
+ """
51
+ print(f"\n{Back.RED}{Style.BRIGHT} ERROR {label} {Style.RESET_ALL}{TAB_BLOCK}{Fore.RED}{msg}")
52
+
53
+ @staticmethod
54
+ def pretty_exception(exception: Exception) -> str:
55
+ """Pretty print an exception.
56
+
57
+ :param exception: Exception to pretty print.
58
+ :type exception: Exception
59
+ :return: Pretty printed exception.
60
+ :rtype: str
61
+ """
62
+ return f"{type(exception).__name__}: {exception}"
63
+
64
+ @staticmethod
65
+ def exception_error_print(label: str, exception: Exception) -> None:
66
+ """Print an error message with exception details to the console.
67
+
68
+ :param label: Label for the error message.
69
+ :type label: str
70
+ :param exception: Exception to print.
71
+ :type exception: Exception
72
+ """
73
+ Console.labeled_error_print(label, Console.pretty_exception(exception))
74
+ print_exc()
75
+
76
+ def debug_print(self, label: str, msg: str) -> None:
77
+ """Print a debug message to the console.
78
+
79
+ :param label: Label for the debug message.
80
+ :type label: str
81
+ :param msg: Debug message to print.
82
+ :type msg: str
83
+ """
84
+ if self._enable_debug:
85
+ self._repeat_print(f"{Back.BLUE}{Style.BRIGHT} DEBUG {label} {Style.RESET_ALL}{TAB_BLOCK}{Fore.BLUE}{msg}")
86
+
87
+ @staticmethod
88
+ def info_print(label: str, msg: str) -> None:
89
+ """Print info to console.
90
+
91
+ :param label: Label for the message.
92
+ :type label: str
93
+ :param msg: Message to print.
94
+ :type msg: str
95
+ """
96
+ print(f"\n{Back.GREEN}{Style.BRIGHT} {label} {Style.RESET_ALL}{TAB_BLOCK}{Fore.GREEN}{msg}")
97
+
98
+ # Helper methods.
99
+ def _repeat_print(self, msg: str) -> None:
100
+ """Print a message to the console with repeat counter.
101
+
102
+ :param msg: Message to print.
103
+ :type msg: str
104
+ """
105
+ if msg == self._last_message:
106
+ self._repeat_counter += 1
107
+ else:
108
+ self._repeat_counter = 1
109
+ self._last_message = msg
110
+ print()
111
+
112
+ print(f"\r{msg}{f" (x{self._repeat_counter})" if self._repeat_counter > 1 else ""}", end="")