BaseRegionOfInterest#

class movement.roi.base.BaseRegionOfInterest(points, dimensions=2, closed=False, holes=None, name=None)[source]#

Bases: object

Base class for regions of interest (RoIs).

Regions of interest can be either 1 or 2 dimensional, and are represented by corresponding shapely.Geometry objects.

To avoid the complexities of subclassing shapely objects (due to them relying on __new__() methods, see shapely/shapely#1233), we simply wrap the relevant shapely object in the _shapely_geometry attribute of the class. This is accessible via the property region. This also allows us to forbid certain operations and make the manipulation of shapely objects 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_allocentric_angle_to_nearest_point(...)

Compute the allocentric angle to the nearest point in the region.

compute_approach_vector(point[, ...])

Compute the approach vector from a point to the region.

compute_distance_to(point[, boundary_only])

Compute the distance from the region to a point.

compute_egocentric_angle_to_nearest_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.

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 position is.

The allocentric angle is the signed angle between the approach vector and a given reference vector.

Parameters:
  • position (xarray.DataArray) – DataArray of spatial positions.

  • boundary_only (bool) – If True, the allocentric angle to the closest boundary point of the region is computed. Default False.

  • in_degrees (bool) – If True, angles are returned in degrees. Otherwise angles are returned in radians. Default False.

  • 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:

float

See also

compute_approach_vector

The method used to compute the approach vector.

compute_egocentric_angle_to_nearest_point

Related class method for computing the egocentric angle to the region.

movement.utils.vector.compute_signed_angle_2d

The 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 point to the region.

The approach vector is defined as the vector directed from the point provided, 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. Default False.

  • unit (bool) – If True, the approach vector is returned normalised, otherwise it is not normalised. Default is False.

Returns:

Approach vector from the point to the region.

Return type:

np.ndarray

See also

compute_allocentric_angle_to_nearest_point

Relies on this method to compute the approach vector.

compute_egocentric_angle_to_nearest_point

Relies 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 from point to the boundary of the region, rather than the closest point belonging to the region. Default False.

Returns:

Euclidean distance from the point to the (closest point on the) region.

Return type:

float

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 direction vector (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 direction vector.

  • boundary_only (bool) – Passed to compute_approach_vector (see Notes). Default False.

  • in_degrees (bool) – If True, angles are returned in degrees. Otherwise angles are returned in radians. Default False.

Return type:

DataArray

See also

compute_allocentric_angle_to_nearest_point

Related class method for computing the egocentric angle to the region.

compute_approach_vector

The method used to compute the approach vector.

movement.utils.vector.compute_signed_angle_2d

The 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 to position that is on the boundary of self. Default False.

Returns:

Coordinates of the point on self that is closest to position.

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 position provided is within the region of interest. False otherwise.

Return type:

bool

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 dimensions: int#

Dimensionality of the region.

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.

property name: str#

Name of the instance.

property region: LinearRing | LineString | Polygon#

shapely.Geometry representation of the region.