BaseRegionOfInterest#
- class movement.roi.BaseRegionOfInterest(points, dimensions=2, closed=False, holes=None, name=None)[source]#
Bases:
objectBase class for regions of interest (RoIs).
Regions of interest can be either 1 or 2 dimensional, and are represented by corresponding
shapely.Geometryobjects.To avoid the complexities of subclassing
shapelyobjects (due to them relying on__new__()methods, see shapely/shapely#1233), we simply wrap the relevantshapelyobject in the_shapely_geometryattribute of the class. This is accessible via the propertyregion. This also allows us to forbid certain operations and make the manipulation ofshapelyobjects more user friendly.Although this class can be instantiated directly, it is not designed for this. Its primary purpose is to reduce code duplication.
Notes
A region of interest includes the points that make up its boundary and the points contained in its interior. This convention means that points inside the region will be treated as having zero distance to the region, and the approach vector from these points to the region will be the null vector.
This may be undesirable in certain situations, when we explicitly want to find the distance of a point to the boundary of a region, for example. To accommodate this, most methods of this class accept a keyword argument that forces the method to perform all computations using only the boundary of the region, rather than the region itself. For polygons, this will force the method in question to only consider distances or closest points on the segments that form the (interior and exterior) boundaries. For segments, the boundary is considered to be just the two endpoints of the segment.
Methods
Compute the allocentric angle to the nearest point in the region.
compute_approach_vector(point[, ...])Compute the approach vector from a
pointto the region.compute_distance_to(point[, boundary_only])Compute the distance from the region to a point.
Compute the egocentric angle to the nearest point in the region.
compute_nearest_point_to(position[, ...])Compute (one of) the nearest point(s) in the region to
position.contains_point(position[, include_boundary])Determine if a position is inside the region of interest.
plot([ax])Plot the region of interest on a new or existing axis.
- compute_allocentric_angle_to_nearest_point(position, boundary_only=False, in_degrees=False, reference_vector=None)[source]#
Compute the allocentric angle to the nearest point in the region.
With the term “allocentric”, we indicate that we are measuring angles with respect to a reference frame that is fixed relative to the experimental/camera setup. By default, we assume this is the positive x-axis of the coordinate system in which
positionis.The allocentric angle is the
signed anglebetween the approach vector and a given reference vector.- Parameters:
position (xarray.DataArray) –
DataArrayof spatial positions.boundary_only (bool) – If
True, the allocentric angle to the closest boundary point of the region is computed. DefaultFalse.in_degrees (bool) – If
True, angles are returned in degrees. Otherwise angles are returned in radians. DefaultFalse.reference_vector (ArrayLike | xr.DataArray) – The reference vector to be used. Dimensions must be compatible with the argument of the same name that is passed to
compute_signed_angle_2d(). Default(1., 0.).
- Return type:
See also
compute_approach_vectorThe method used to compute the approach vector.
compute_egocentric_angle_to_nearest_pointRelated class method for computing the egocentric angle to the region.
movement.utils.vector.compute_signed_angle_2dThe underlying function used to compute the signed angle between the approach vector and the reference vector.
- compute_approach_vector(point, boundary_only=False, unit=False)[source]#
Compute the approach vector from a
pointto the region.The approach vector is defined as the vector directed from the
pointprovided, to the closest point that belongs to the region.- Parameters:
point (ArrayLike) – Coordinates of a point to compute the vector to (or from) the region.
boundary_only (bool) – If
True, the approach vector to the boundary of the region is computed. DefaultFalse.unit (bool) – If
True, the approach vector is returned normalised, otherwise it is not normalised. Default isFalse.
- Returns:
Approach vector from the point to the region.
- Return type:
np.ndarray
See also
compute_allocentric_angle_to_nearest_pointRelies on this method to compute the approach vector.
compute_egocentric_angle_to_nearest_pointRelies on this method to compute the approach vector.
- compute_distance_to(point, boundary_only=False)[source]#
Compute the distance from the region to a point.
- Parameters:
point (ArrayLike) – Coordinates of a point, from which to find the nearest point in the region defined by
self.boundary_only (bool, optional) – If
True, compute the distance frompointto the boundary of the region, rather than the closest point belonging to the region. DefaultFalse.
- Returns:
Euclidean distance from the
pointto the (closest point on the) region.- Return type:
- compute_egocentric_angle_to_nearest_point(direction, position, boundary_only=False, in_degrees=False)[source]#
Compute the egocentric angle to the nearest point in the region.
With the term “egocentric”, we indicate that we are measuring angles with respect to a reference frame that is varying in time relative to the experimental/camera setup.
The egocentric angle is the signed angle between the approach vector and a
directionvector (examples include the forward vector of a given individual, or the velocity vector of a given point).- Parameters:
direction (xarray.DataArray) – An array of vectors representing a given direction, e.g., the forward vector(s).
position (xarray.DataArray) – DataArray of spatial positions, considered the origin of the
directionvector.boundary_only (bool) – Passed to
compute_approach_vector()(see Notes). DefaultFalse.in_degrees (bool) – If
True, angles are returned in degrees. Otherwise angles are returned in radians. DefaultFalse.
- Return type:
See also
compute_allocentric_angle_to_nearest_pointRelated class method for computing the egocentric angle to the region.
compute_approach_vectorThe method used to compute the approach vector.
movement.utils.vector.compute_signed_angle_2dThe underlying function used to compute the signed angle between the approach vector and the reference vector.
- compute_nearest_point_to(position, boundary_only=False)[source]#
Compute (one of) the nearest point(s) in the region to
position.If there are multiple equidistant points, one of them is returned.
- Parameters:
position (ArrayLike) – Coordinates of a point, from which to find the nearest point in the region.
boundary_only (bool, optional) – If
True, compute the nearest point topositionthat is on the boundary ofself. DefaultFalse.
- Returns:
Coordinates of the point on
selfthat is closest toposition.- Return type:
np.ndarray
- contains_point(position, include_boundary=True)[source]#
Determine if a position is inside the region of interest.
- Parameters:
position (ArrayLike) – Spatial coordinates [x, y, [z]] to check as being inside the region.
include_boundary (bool) – Whether to treat a position on the region’s boundary as inside the region (True) or outside the region (False). Default True.
- Returns:
True if the
positionprovided is within the region of interest. False otherwise.- Return type:
- property coords: CoordinateSequence#
Coordinates of the points that define the region.
These are the points passed to the constructor argument
points.Note that for Polygonal regions, these are the coordinates of the exterior boundary, interior boundaries must be accessed via
self.region.interior.coords.
- property is_closed: bool#
Return True if the region is closed.
A closed region is either: - A polygon (2D RoI). - A 1D LoI whose final point connects back to its first.
- plot(ax=None, **matplotlib_kwargs)[source]#
Plot the region of interest on a new or existing axis.
- Parameters:
ax (plt.Axes, optional) –
matplotlib.pyplot.Axesobject to draw the region on. A newFigureandAxeswill be created if not provided.matplotlib_kwargs (Any) – Keyword arguments passed to the
matplotlib.pyplotplotting function.
- Return type:
- property region: LinearRing | LineString | Polygon#
shapely.Geometryrepresentation of the region.
Examples using BaseRegionOfInterest#
Compute distances and angles to regions of interest