Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alibylite
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Swain Lab
aliby
alibylite
Commits
e73797ca
Commit
e73797ca
authored
2 years ago
by
Alán Muñoz
Browse files
Options
Downloads
Patches
Plain Diff
tweak(extractor): pick and choose between bn, np
parent
d99e55a3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/extraction/core/functions/cell.py
+18
-12
18 additions, 12 deletions
src/extraction/core/functions/cell.py
with
18 additions
and
12 deletions
src/extraction/core/functions/cell.py
+
18
−
12
View file @
e73797ca
...
@@ -2,6 +2,12 @@
...
@@ -2,6 +2,12 @@
Base functions to extract information from a single cell
Base functions to extract information from a single cell
These functions are automatically read by extractor.py, and so can only have the cell_mask and trap_image as inputs and must return only one value.
These functions are automatically read by extractor.py, and so can only have the cell_mask and trap_image as inputs and must return only one value.
They assume that there are no NaNs in the image.
We use bottleneck when it performs faster than numpy:
- Median
- values containing NaNs (We make sure this does not happen)
"""
"""
import
math
import
math
import
typing
as
t
import
typing
as
t
...
@@ -21,7 +27,7 @@ def area(cell_mask) -> int:
...
@@ -21,7 +27,7 @@ def area(cell_mask) -> int:
cell_mask: 2d array
cell_mask: 2d array
Segmentation mask for the cell
Segmentation mask for the cell
"""
"""
return
bn
.
nan
sum
(
cell_mask
)
return
np
.
sum
(
cell_mask
)
def
eccentricity
(
cell_mask
)
->
float
:
def
eccentricity
(
cell_mask
)
->
float
:
...
@@ -47,7 +53,7 @@ def mean(cell_mask, trap_image) -> float:
...
@@ -47,7 +53,7 @@ def mean(cell_mask, trap_image) -> float:
Segmentation mask for the cell
Segmentation mask for the cell
trap_image: 2d array
trap_image: 2d array
"""
"""
return
bn
.
nan
mean
(
trap_image
[
cell_mask
])
return
np
.
mean
(
trap_image
[
cell_mask
])
def
median
(
cell_mask
,
trap_image
)
->
int
:
def
median
(
cell_mask
,
trap_image
)
->
int
:
...
@@ -60,7 +66,7 @@ def median(cell_mask, trap_image) -> int:
...
@@ -60,7 +66,7 @@ def median(cell_mask, trap_image) -> int:
Segmentation mask for the cell
Segmentation mask for the cell
trap_image: 2d array
trap_image: 2d array
"""
"""
return
bn
.
nan
median
(
trap_image
[
cell_mask
])
return
bn
.
median
(
trap_image
[
cell_mask
])
def
max2p5pc
(
cell_mask
,
trap_image
)
->
float
:
def
max2p5pc
(
cell_mask
,
trap_image
)
->
float
:
...
@@ -74,14 +80,14 @@ def max2p5pc(cell_mask, trap_image) -> float:
...
@@ -74,14 +80,14 @@ def max2p5pc(cell_mask, trap_image) -> float:
trap_image: 2d array
trap_image: 2d array
"""
"""
# number of pixels in mask
# number of pixels in mask
npixels
=
bn
.
nan
sum
(
cell_mask
)
npixels
=
np
.
sum
(
cell_mask
)
n_top
=
int
(
np
.
ceil
(
npixels
*
0.025
))
n_top
=
int
(
np
.
ceil
(
npixels
*
0.025
))
# sort pixels in cell and find highest 2.5%
# sort pixels in cell and find highest 2.5%
pixels
=
trap_image
[
cell_mask
]
pixels
=
trap_image
[
cell_mask
]
top_values
=
bn
.
partition
(
pixels
,
len
(
pixels
)
-
n_top
)[
-
n_top
:]
top_values
=
bn
.
partition
(
pixels
,
len
(
pixels
)
-
n_top
)[
-
n_top
:]
# find mean of these highest pixels
# find mean of these highest pixels
return
bn
.
nan
mean
(
top_values
)
return
np
.
mean
(
top_values
)
def
max5px
(
cell_mask
,
trap_image
)
->
float
:
def
max5px
(
cell_mask
,
trap_image
)
->
float
:
...
@@ -98,7 +104,7 @@ def max5px(cell_mask, trap_image) -> float:
...
@@ -98,7 +104,7 @@ def max5px(cell_mask, trap_image) -> float:
pixels
=
trap_image
[
cell_mask
]
pixels
=
trap_image
[
cell_mask
]
top_values
=
bn
.
partition
(
pixels
,
len
(
pixels
)
-
5
)[
-
5
:]
top_values
=
bn
.
partition
(
pixels
,
len
(
pixels
)
-
5
)[
-
5
:]
# find mean of five brightest pixels
# find mean of five brightest pixels
max5px
=
bn
.
nan
mean
(
top_values
)
max5px
=
np
.
mean
(
top_values
)
return
max5px
return
max5px
...
@@ -112,7 +118,7 @@ def std(cell_mask, trap_image):
...
@@ -112,7 +118,7 @@ def std(cell_mask, trap_image):
Segmentation mask for the cell
Segmentation mask for the cell
trap_image: 2d array
trap_image: 2d array
"""
"""
return
bn
.
nan
std
(
trap_image
[
cell_mask
])
return
np
.
std
(
trap_image
[
cell_mask
])
def
k2_major_median
(
cell_mask
,
trap_image
):
def
k2_major_median
(
cell_mask
,
trap_image
):
...
@@ -137,12 +143,12 @@ def k2_major_median(cell_mask, trap_image):
...
@@ -137,12 +143,12 @@ def k2_major_median(cell_mask, trap_image):
indices
=
faiss
.
IndexFlatL2
(
X
.
shape
[
1
])
indices
=
faiss
.
IndexFlatL2
(
X
.
shape
[
1
])
# (n_clusters=2, random_state=0).fit(X)
# (n_clusters=2, random_state=0).fit(X)
_
,
indices
=
indices
.
search
(
X
,
k
=
2
)
_
,
indices
=
indices
.
search
(
X
,
k
=
2
)
high_indices
=
bn
.
nan
argmax
(
indices
,
axis
=
1
).
astype
(
bool
)
high_indices
=
np
.
argmax
(
indices
,
axis
=
1
).
astype
(
bool
)
# find the median of pixels in the largest cluster
# find the median of pixels in the largest cluster
# high_masks = np.logical_xor( # Use casting to obtain masks
# high_masks = np.logical_xor( # Use casting to obtain masks
# high_indices.reshape(-1, 1), np.tile((0, 1), X.shape[0]).reshape(-1, 2)
# high_indices.reshape(-1, 1), np.tile((0, 1), X.shape[0]).reshape(-1, 2)
# )
# )
major_median
=
bn
.
nan
median
(
X
[
high_indices
])
major_median
=
bn
.
median
(
X
[
high_indices
])
return
major_median
return
major_median
...
@@ -172,7 +178,7 @@ def conical_volume(cell_mask):
...
@@ -172,7 +178,7 @@ def conical_volume(cell_mask):
nearest_neighbor
=
(
nearest_neighbor
=
(
ndimage
.
morphology
.
distance_transform_edt
(
padded
==
1
)
*
padded
ndimage
.
morphology
.
distance_transform_edt
(
padded
==
1
)
*
padded
)
)
return
4
*
(
nearest_neighbor
.
sum
()
)
return
4
*
np
.
sum
(
nearest_neighbor
)
def
spherical_volume
(
cell_mask
):
def
spherical_volume
(
cell_mask
):
...
@@ -207,8 +213,8 @@ def min_maj_approximation(cell_mask) -> t.Tuple[int]:
...
@@ -207,8 +213,8 @@ def min_maj_approximation(cell_mask) -> t.Tuple[int]:
# get the size of the top of the cone (points that are equally maximal)
# get the size of the top of the cone (points that are equally maximal)
cone_top
=
ndimage
.
morphology
.
distance_transform_edt
(
dn
==
0
)
*
padded
cone_top
=
ndimage
.
morphology
.
distance_transform_edt
(
dn
==
0
)
*
padded
# minor axis = largest distance from the edge of the ellipse
# minor axis = largest distance from the edge of the ellipse
min_ax
=
np
.
round
(
bn
.
nan
max
(
nn
))
min_ax
=
np
.
round
(
np
.
max
(
nn
))
# major axis = largest distance from the cone top
# major axis = largest distance from the cone top
# + distance from the center of cone top to edge of cone top
# + distance from the center of cone top to edge of cone top
maj_ax
=
np
.
round
(
bn
.
nan
max
(
dn
)
+
bn
.
nan
sum
(
cone_top
)
/
2
)
maj_ax
=
np
.
round
(
np
.
max
(
dn
)
+
np
.
sum
(
cone_top
)
/
2
)
return
min_ax
,
maj_ax
return
min_ax
,
maj_ax
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment