cortical_sanity module¶
- class cortical_sanity.CorticalSanityCheck(MIN_THICKNESS: float, ext_contour: ndarray, int_contour: ndarray, model: str, save_plot: bool, logger: Logger)[source]¶
Bases:
object
- KDT_nn(p, arr)[source]¶
Find nearest point between two arrays and create an array containing nearest-pairs indices
- Parameters:
arr1 (np.ndarray) – array of contour 1
arr2 (np.ndarray) – array of contour 2
- Returns:
_description_
- Return type:
_type_
- ccw_angle(array1, array2, idx1, idx2)[source]¶
Returns the angle between two 2D arrays in the range [0, 2*pi)
- Parameters:
array1 (numpy.ndarray) – array of shape (2,) containing [x, y] coords
array2 (numpy.ndarray) – array of shape (2,) containing [x, y] coords
- Returns:
array of shape (1,) containing angles between array1 and array2
- Return type:
numpy.ndarray
- center_of_mass(array: ndarray)[source]¶
Calculate the center of mass of a 2D array.
This function computes the center of mass (centroid) of a given 2D array. The center of mass is calculated based on the sum of the array values and their respective coordinates.
- Parameters:
array (np.ndarray) – A 2D NumPy array representing the data.
- Returns:
The x and y coordinates of the center of mass.
- Return type:
Tuple[float, float]
- check_min_thickness(ext, int, idx_ext, idx_int, min_thickness=1)[source]¶
# ! delete this function if not used Checks thickness of ext, int arrays and returns a list of booleans of form [True, …, False] where True means the thickness if below tolerance.
- Parameters:
ext (numpy.ndarray) – array of [x, y] points of external polygon
int (numpy.ndarray) – array of [x, y] points of internal polygon
min_thickness (float) – minimum thickness tolerance between ext/int
- Returns:
list indicating where the thickness is below tolerance
- Return type:
bool_min_thickness (list)
- correct_internal_point(arr, base_idx, dx, dy)[source]¶
Corrects [x, y] position of points of internal array
- Parameters:
arr (ndarray) – array of internal perimeter
dx (float) – normal component x * minimum thickness
dy (float) – normal component y * minimum thickness
- Returns:
new position of internal points in array ‘arr’
- Return type:
ndarray
- correct_intersection(ext_arr, int_arr, base_idx, dx, dy, bool_arr)[source]¶
Takes ext and int arrays and applies nodal displacement to the elements where bool_min_thickness == True
- Parameters:
ext_arr (ndarray) – array of [x, y] external points composing the perimeter
int_arr (ndarray) – array of [x, y] internal points composing the perimeter
dx (float) – normal component x * minimum thickness
dy (float) – normal component y * minimum thickness
bool_min_thickness (list) – list indicating where the thickness is below tolerance
- Returns:
new array of internal polygon
- Return type:
ndarray
- cortical_sanity_check(ext_contour: ndarray, int_contour: ndarray, iterator: int, show_plots: bool = True) ndarray [source]¶
Check if the internal contour is within the external contour.
- Parameters:
ext_contour (ndarray) – 2D array of [x, y] points
int_contour (ndarray) – 2D array of [x, y] points
Returns:
- draw_arrow(ax, arr_start, arr_end, text, color)[source]¶
Helper that draws normals with arrow and vector annotation
- Parameters:
ax (AxesSubplot) – ax where to add the arrow
arr_start (tuple) – tuple of arrow starting point
arr_end (tuple) – tuple of arrow ending point
text (str) – string of text to add as annotation
color (str) – color of arrow + annotation
- get_normals(xs, ys, ax1, ax2, thickness=1, debug=False)[source]¶
Get normal arrays point-wise for array [xs, ys]
- Parameters:
xs (ndarray) – x-component of contour array
ys (ndarray) – y-component of contour array
ax1 (AxesSubplot) – ax subplot 1
ax2 (AxesSubplot) – ax subplot 2
thickness (int, optional) – Minimum thickness of int-ext interface. Defaults to 1.
- Returns:
array of dx component of the normals dy_arr (ndarray): array of dy component of the normals dx_med (ndarray): array of resultant normal between (x_n and x_n+1) dy_med (ndarray): array of resultant normal between (y_n and y_n+1)
- Return type:
dx_arr (ndarray)
- hyp_min_thickness(ext_contour, int_contour, idx_)[source]¶
Check if the thickness between external and internal contours is below a minimum threshold.
This function evaluates the thickness between corresponding points on the external and internal contours. It checks if the thickness at any point is below a specified minimum threshold and returns a boolean array indicating where this condition is met.
- Parameters:
ext_contour (ndarray) – The external contour points.
int_contour (ndarray) – The internal contour points.
idx (ndarray) – Indices for the internal contour points.
- Returns:
A boolean array indicating where the thickness is below the minimum threshold.
- Return type:
ndarray
- is_angle_bigger_bool(alpha_int, alpha_ext)[source]¶
Returns True if alpha_int is bigger than alpha_ext, False otherwise If alpha_int is bigger than alpha_ext, then the internal angle is bigger than the external angle If np.nan, then return False and print a warning Else, return False
- Parameters:
alpha_int (numpy.ndarray) – array of shape (1,) containing internal angles
alpha_ext (numpy.ndarray) – array of shape (1,) containing external angles
- Raises:
ValueError – print a warning if alpha_int or alpha_ext is np.nan (0-degree angle)
RuntimeWarning – error if comparison is not possible
- Returns:
array containing True if alpha_int is bigger than alpha_ext, False otherwise
- Return type:
numpy.ndarray
- is_internal_inside_external(p_e_0, p_i_0, idx_ext, idx_int)[source]¶
Rationale for deciding if internal contour is inside external contour based on the angle between the first and second point of the external contour, and the first point and the first point of the internal contour.
- Parameters:
p_e_0 (numpy.ndarray) – external contour
p_i_0 (numpy.ndarray) – internal contour
- Returns:
Booleans where True if internal contour is OUTSIDE external contour, False otherwise
- Return type:
list
pseudo-code: Pe: Point of external perimeter Pi: Point of internal perimeter P0: First point P1: Pivot point P2: Second point # Compute vectors between Pe0-Pe1 and Pe1-Pe2 # v1_e_u = ext[i+1] - ext[i] # v2_e_u = ext[i+2] - ext[i+1]
# Compute angles between vectors # alpha_e = angle_between(v1_e_u, v2_e_u) # alpha_i = angle_between(v1_e_u, v2_i_u)
- is_internal_radius_bigger_than_external(p_e_0, p_i_0, idx_ext, idx_int)[source]¶
Returns True if the internal radius is bigger than the external radius, False otherwise
- Parameters:
p_e_0 (numpy.ndarray) – array of shape (2,) containing external radius
p_i_0 (numpy.ndarray) – array of shape (2,) containing internal radius
- Returns:
True if internal radius is bigger than external radius, False otherwise
- Return type:
bool_radius
- lineseg_dists(p, a, b)[source]¶
https://stackoverflow.com/questions/27161533/find-the-shortest-distance-between-a-point-and-line-segments-not-line Cartesian distance from point to line segment
Edited to support arguments as series, from: https://stackoverflow.com/a/54442561/11208892
- Parameters:
p (-) – np.array of single point, shape (2,) or 2D array, shape (x, 2)
a (-) – np.array of shape (x, 2)
b (-) – np.array of shape (x, 2)
- nearest_pairs_arrs(arr1, arr2)[source]¶
Find nearest point between two arrays and create a an array containing nearest-pairs indices
- Parameters:
arr1 (np.ndarray) – array of contour 1 (e.g. ext)
arr2 (np.ndarray) – array of contour 2 (e.g. int)
- Returns:
array of indices of nearest index in arr2 for each point in arr1 base_idx (np.ndarray): array of ordered indices of arr1
- Return type:
closest_idx (np.ndarray)
- nearest_point(arr, pt)[source]¶
Find nearest point between an array and a point (e.g. the first point of ext contour)
- Parameters:
arr (ndarray) – 2D array of contour
pt (list) – (x, y) coordinates of point
- Returns:
location of nearest point in the array dist (float): distance between point and nearest point in array idx (numpy.int64): index of point in array
- Return type:
loc (numpy.ndarray)
- offset_surface(line: ndarray, offset: float) ndarray [source]¶
Create artificial internal surface based on an offset :param np.ndarray: 2D array of [x, y] points of the external surface :param offset: offset distance :type offset: float
- Returns:
2D array of [x, y] points of the offset surface
- Return type:
np.ndarray
- plot_contours(min_thickness: float, x_ext: ndarray, y_ext: ndarray, x_int: ndarray, y_int: ndarray, debug: bool = False)[source]¶
Create contours, get normals, plot
- Parameters:
min_thickness (int) – Minimum thickness of int-ext interface.
x_ext (ndarray) – array of [x] component of external array
y_ext (ndarray) – array of [y] component of external array
x_int (ndarray) – array of [x] component of internal array
y_int (ndarray) – array of [y] component of internal array
- Returns:
2D array of [x, y] points int_a (ndarray): 2D array of [x, y] points dx_arr (ndarray): array of dx component of the normals dy_arr (ndarray): array of dy component of the normals dx_med (ndarray): array of resultant normal between (x_n and x_n+1) dy_med (ndarray): array of resultant normal between (y_n and y_n+1) fig (matplotlib.figure.Figure): figure ax1 (AxesSubplot): ax subplot ax2 (AxesSubplot): ax subplot
- Return type:
ext_a (ndarray)
- plot_corrected_contours(fig, ax1, ax2, ext_s, dx_med, dy_med, ext_spline, ext_offset, int_spline, new_int, iterator, save=False)[source]¶
Plot and optionally save corrected contours for cortical thickness analysis.
This function plots the corrected internal and external contours for cortical thickness analysis. It also provides an option to save the plots and the corresponding data to files.
- Parameters:
fig (matplotlib.figure.Figure) – The figure object for the plot.
ax1 (matplotlib.axes.Axes) – The first axes object for the plot.
ax2 (matplotlib.axes.Axes) – The second axes object for the plot.
ext_s (ndarray) – The external contour points.
dx_med (float) – The x-offset for the external contour.
dy_med (float) – The y-offset for the external contour.
ext_spline (ndarray) – The external spline contour points.
ext_offset (ndarray) – The offset external contour points.
int_spline (ndarray) – The internal spline contour points.
new_int (ndarray) – The corrected internal contour points.
iterator (int) – The current iteration index.
save (bool, optional) – Whether to save the plot and data to files. Default is False.
- Returns:
None
- push_contour(ext_contour: ndarray, int_contour: ndarray, offset: float) Tuple[ndarray, ndarray] [source]¶
Adjust the internal contour to ensure it is within the offset external contour.
This function adjusts the internal contour points to ensure they are within the offset external contour. It resamples both contours to a higher resolution, checks if the internal points are within the external contour, and if not, moves them to the closest points on the external contour.
- Parameters:
ext_contour (np.ndarray) – The external contour points.
int_contour (np.ndarray) – The internal contour points.
offset (float) – The offset distance for the external contour.
- Returns:
The adjusted internal contour and the offset external contour.
- Return type:
Tuple[np.ndarray, np.ndarray]
- resample_contour(xy: ndarray, n_points: int = 100) ndarray [source]¶
Cumulative Euclidean distance between successive polygon points.
- Parameters:
xy (np.ndarray) – 2D-array of [x, y] contour
n_points (int, optional) – Number of points to resample contour. Usually len(ext contour). Defaults to 100.
- Returns:
Resampled contour [x, y]
- Return type:
np.ndarray
- reset_numpy_index(arr, idx=1)[source]¶
Reset the index of a numpy array to a given index Legacy function, use numpy_roll_index() instead :param arr: entry array :type arr: numpy.ndarray :param idx: index to reset to. Defaults to 1. :type idx: int
- Returns:
numpy array with index reset to idx
- Return type:
numpy.ndarray