matplotlib_polyroi package

Module contents

class matplotlib_polyroi.ROI_jupyter(img: Union[ndarray, list], spline: bool = True, start_frame: int = 0, end_frame: Optional[int] = None, periodic: bool = True, show_fit: bool = True, k: int = 3)

Bases: object

There are two different methods for drawing ROIs depending on if you’re using normal python scripts or Jupyter notebooks. This is the one to use if you’re using Jupyter notebooks.

Example workflow:

### Cell 1:

r = RoiJupyter(img, periodic=True, spline=True)

r.run()

# A window will appear - draw the ROI and click save

### Cell 2:

roi = r.roi

print(roi.shape)

# Confirm that the shape is not (0, 0)

To save this in a fiji readable format: np.savetxt(filename, roi, fmt=’%.4f’, delimiter=’ ‘)

Parameters
  • img – image (2D or 3D numpy array)

  • spline – if True, will fit a spline to the user-defined points

  • start_frame – if img is a time series, this is the index of the first frame to show

  • end_frame – if img is a time series, this is the index of the last frame to show

  • periodic – set to True if drawing a periodic ROI

  • show_fit – if True, will show the spline fit (if spline=True)

  • k – degree of the spline (e.g. 3 = cubic)

run()
matplotlib_polyroi.def_roi(stack: Union[ndarray, list], spline: bool = True, start_frame: int = 0, end_frame: Optional[int] = None, periodic: bool = True, show_fit: bool = True, k: int = 3)

There are two different methods for drawing ROIs depending on if you’re using normal python scripts or Jupyter notebooks. This is the one to use if you’re using python scripts.

Instructions:

  • click to lay down points

  • backspace at any time to remove last point

  • press enter to select area (if spline=True will fit spline to points, otherwise will fit straight lines)

  • at this point can press backspace to go back to laying points

  • press enter again to close and return ROI

Parameters
  • stack – image (2D or 3D numpy array)

  • spline – if True, will fit a spline to the user-defined points

  • start_frame – if img is a time series, this is the index of the first frame to show

  • end_frame – if img is a time series, this is the index of the last frame to show

  • periodic – set to True if drawing a periodic ROI

  • show_fit – if True, will show the spline fit (if spline=True)

  • k – degree of the spline (e.g. 3 = cubic)

Returns

ROI as a numpy array

To save this in a fiji readable format: np.savetxt(filename, roi, fmt=’%.4f’, delimiter=’ ‘)

matplotlib_polyroi.interp_roi(roi: ndarray, periodic: bool = True, npoints: Optional[int] = None, gap: int = 1) ndarray

Interpolates coordinates to one pixel distances (or as close as possible to one pixel). Linear interpolation

Parameters
  • roi – two column array containing x and y coordinates. e.g. roi = np.loadtxt(filename)

  • periodic – set to True if the ROI is periodic

  • npoints – number of points to interpolate to

  • gap – alternatively, specify the desired gap between succesive coordinates in pixel units

Returns

interpolated ROI (numpy array)

matplotlib_polyroi.load_image(filename: str) ndarray

Given the filename of a TIFF, creates numpy array with pixel intensities

Parameters

filename – full path to the file to import (including extension)

Returns

A numpy array of the image

matplotlib_polyroi.offset_coordinates(roi: ndarray, offsets: Union[ndarray, float], periodic: bool = True) ndarray

Reads in coordinates, adjusts according to offsets

Parameters
  • roi – two column array containing x and y coordinates. e.g. roi = np.loadtxt(filename)

  • offsets – array the same length as coors. Direction?

  • periodic – set to True if the ROI is periodic

Returns

array in same format as coors containing new coordinates.

To save this in a fiji readable format: np.savetxt(filename, newcoors, fmt=’%.4f’, delimiter=’ ‘)

matplotlib_polyroi.spline_roi(roi: ndarray, periodic: bool = True, s: float = 0.0, k: int = 3) ndarray

Fits a spline to points specifying the coordinates of the cortex, then interpolates to pixel distances

Parameters
  • roi – two column array containing x and y coordinates. e.g. roi = np.loadtxt(filename)

  • periodic – set to True if the ROI is periodic

  • s – splprep s parameter

  • k – splprep k parameter (spline order)

Returns

spline ROI (numpy array)