Skip to content

Commit 329a108

Browse files
committed
Improved docstrings, type hints etc.
1 parent 027785a commit 329a108

7 files changed

Lines changed: 415 additions & 207 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/example_datasets/

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# saibr_python
22

3-
Python implementation of SAIBR
3+
Python implementation of SAIBR
4+
5+
6+
conda create -n saibr python=3.7 anaconda=2020.11
7+
conda activate saibr
8+
pip install opencv-python

example_datasets.zip

30.8 MB
Binary file not shown.

saibr/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .saibr import *
22
from .funcs import *
3+
from .roi import *

saibr/funcs.py

Lines changed: 7 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,22 @@
11
import numpy as np
22
from skimage import io
33
import cv2
4-
from scipy.interpolate import splprep, splev, interp1d
54

65

7-
def load_image(filename):
6+
def load_image(filename: str) -> np.ndarray:
87
"""
98
Given the filename of a TIFF, creates numpy array with pixel intensities
109
11-
:param filename:
12-
:return:
13-
"""
14-
15-
# img = np.array(Image.open(filename), dtype=np.float64)
16-
# img[img == 0] = np.nan
17-
return io.imread(filename).astype(float)
18-
19-
20-
def offset_coordinates(roi, offsets):
21-
"""
22-
Reads in coordinates, adjusts according to offsets
23-
24-
:param roi: two column array containing x and y coordinates. e.g. coors = np.loadtxt(filename)
25-
:param offsets: array the same length as coors. Direction?
26-
:return: array in same format as coors containing new coordinates
10+
Args:
11+
filename: full path to the file to import (including extension)
2712
28-
To save this in a fiji readable format run:
29-
np.savetxt(filename, newcoors, fmt='%.4f', delimiter='\t')
13+
Returns:
14+
A numpy array of the image
3015
3116
"""
3217

33-
# Calculate gradients
34-
xcoors = roi[:, 0]
35-
ycoors = roi[:, 1]
36-
ydiffs = np.diff(ycoors, prepend=ycoors[-1])
37-
xdiffs = np.diff(xcoors, prepend=xcoors[-1])
38-
grad = ydiffs / xdiffs
39-
tangent_grad = -1 / grad
40-
41-
# Offset coordinates
42-
xchange = ((offsets ** 2) / (1 + tangent_grad ** 2)) ** 0.5
43-
ychange = xchange / abs(grad)
44-
newxs = xcoors + np.sign(ydiffs) * np.sign(offsets) * xchange
45-
newys = ycoors - np.sign(xdiffs) * np.sign(offsets) * ychange
46-
newcoors = np.swapaxes(np.vstack([newxs, newys]), 0, 1)
47-
return newcoors
18+
return io.imread(filename).astype(float)
4819

4920

50-
def make_mask(shape, roi):
21+
def make_mask(shape: tuple, roi: np.ndarray) -> np.ndarray:
5122
return cv2.fillPoly(np.zeros(shape) * np.nan, [np.int32(roi)], 1)
52-
53-
54-
def spline_roi(roi, periodic=True, s=0):
55-
"""
56-
Fits a spline to points specifying the coordinates of the cortex, then interpolates to pixel distances
57-
58-
:param roi:
59-
:return:
60-
"""
61-
62-
# Append the starting x,y coordinates
63-
if periodic:
64-
x = np.r_[roi[:, 0], roi[0, 0]]
65-
y = np.r_[roi[:, 1], roi[0, 1]]
66-
else:
67-
x = roi[:, 0]
68-
y = roi[:, 1]
69-
70-
# Fit spline
71-
tck, u = splprep([x, y], s=s, per=periodic)
72-
73-
# Evaluate spline
74-
xi, yi = splev(np.linspace(0, 1, 10000), tck)
75-
76-
# Interpolate
77-
return interp_roi(np.vstack((xi, yi)).T, periodic=periodic)
78-
79-
80-
def interp_roi(roi, periodic=True):
81-
"""
82-
Interpolates coordinates to one pixel distances (or as close as possible to one pixel)
83-
Linear interpolation
84-
85-
:param roi:
86-
:return:
87-
"""
88-
89-
if periodic:
90-
c = np.append(roi, [roi[0, :]], axis=0)
91-
else:
92-
c = roi
93-
94-
# Calculate distance between points in pixel units
95-
distances = ((np.diff(c[:, 0]) ** 2) + (np.diff(c[:, 1]) ** 2)) ** 0.5
96-
distances_cumsum = np.r_[0, np.cumsum(distances)]
97-
total_length = sum(distances)
98-
99-
# Interpolate
100-
fx, fy = interp1d(distances_cumsum, c[:, 0], kind='linear'), interp1d(distances_cumsum, c[:, 1], kind='linear')
101-
positions = np.linspace(0, total_length, int(round(total_length)))
102-
xcoors, ycoors = fx(positions), fy(positions)
103-
newpoints = np.c_[xcoors[:-1], ycoors[:-1]]
104-
return newpoints

0 commit comments

Comments
 (0)