Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
model_server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Christopher Randolph Rhodes
model_server
Commits
9942e931
Commit
9942e931
authored
1 year ago
by
Christopher Randolph Rhodes
Browse files
Options
Downloads
Patches
Plain Diff
zmask docstring, test coverage of expand_box_by param
parent
2bd47356
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
extensions/chaeo/test_zstack.py
+6
-2
6 additions, 2 deletions
extensions/chaeo/test_zstack.py
extensions/chaeo/zmask.py
+31
-19
31 additions, 19 deletions
extensions/chaeo/zmask.py
model_server/accessors.py
+16
-0
16 additions, 0 deletions
model_server/accessors.py
with
53 additions
and
21 deletions
extensions/chaeo/test_zstack.py
+
6
−
2
View file @
9942e931
...
...
@@ -25,12 +25,13 @@ class TestZStackDerivedDataProducts(unittest.TestCase):
self
.
obmap
=
InMemoryDataAccessor
(
self
.
pxmap
.
data
>
pipeline_params
[
'
threshold
'
])
# write_accessor_data_to_file(output_path / 'obmap.tif', self.obmap)
def
test_zmask_makes_correct_boxes
(
self
,
mask_type
=
'
boxes
'
,
filters
=
None
):
def
test_zmask_makes_correct_boxes
(
self
,
mask_type
=
'
boxes
'
,
filters
=
None
,
expand_box_by
=
None
):
zmask
,
meta
=
build_zmask_from_object_mask
(
self
.
obmap
.
get_one_channel_data
(
0
),
self
.
stack
.
get_one_channel_data
(
0
),
mask_type
=
mask_type
,
filters
=
filters
,
expand_box_by
=
expand_box_by
,
)
zmask_acc
=
InMemoryDataAccessor
(
zmask
)
self
.
assertTrue
(
zmask_acc
.
is_mask
())
...
...
@@ -54,4 +55,7 @@ class TestZStackDerivedDataProducts(unittest.TestCase):
return
self
.
test_zmask_makes_correct_boxes
(
mask_type
=
'
contours
'
)
def
test_zmask_makes_correct_boxes_with_filters
(
self
):
return
self
.
test_zmask_makes_correct_boxes
(
filters
=
{
'
area
'
:
(
1e3
,
1e4
)})
\ No newline at end of file
return
self
.
test_zmask_makes_correct_boxes
(
filters
=
{
'
area
'
:
(
1e3
,
1e4
)})
def
test_zmask_makes_correct_expanded_boxes
(
self
):
return
self
.
test_zmask_makes_correct_boxes
(
expand_box_by
=
(
64
,
2
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
extensions/chaeo/zmask.py
+
31
−
19
View file @
9942e931
...
...
@@ -8,27 +8,40 @@ from model_server.accessors import GenericImageDataAccessor
# build a single boolean 3d mask (objects v. bboxes) and return bounding boxes
def
build_zmask_from_object_mask
(
obmask
:
GenericImageDataAccessor
,
stack
:
GenericImageDataAccessor
,
z
stack
:
GenericImageDataAccessor
,
filters
=
None
,
mask_type
=
'
contour
'
,
expand_box_by
=
(
0
,
0
)
expand_box_by
=
(
0
,
0
)
,
):
"""
Given a 2D
filters: dict of (min, max) tuples
expand_box_by: (xy, z) pixelsf
Given a 2D mask of objects, build a 3D mask, where each object
'
s z-position is determined by the index of
maximum intensity in z. Return this zmask and a list of each object
'
s meta information.
:param obmask: GenericImageDataAccessor monochrome 2D inary mask of objects
:param zstack: GenericImageDataAccessor monochrome zstack of same Y, X dimension as obmask
:param filters: dictionary of form {attribute: (min, max)}; valid attributes are
'
area
'
and
'
solidity
'
:param mask_type: if
'
boxes
'
, zmask is True in each object
'
s complete bounding box; otherwise
'
contours
'
:param expand_box_by: (xy, z) expands bounding box by (xy, z) pixels except where this hits a boundary
:return: tuple (zmask, meta)
np.ndarray zmask: boolean mask of same size as stack
meta: List containing one Dict per object, with keys:
info: object
'
s properties from skimage.measure.regionprops_table, including bounding box (y0, y1, x0, x1)
slice: named slice (np.s_) of (optionally) expanded bounding box
relative_bounding_box: bounding box (y0, y1, x0, x1) in relative frame of (optionally) expanded bounding box
contour: object
'
s contour returned by skimage.measure.find_contours
mask: mask of object in relative frame of (optionally) expanded bounding box
"""
# validate inputs
assert
stack
.
chroma
==
1
assert
stack
.
shape_dict
[
'
Z
'
]
>
1
assert
z
stack
.
chroma
==
1
assert
z
stack
.
nz
>
1
assert
mask_type
in
(
'
contours
'
,
'
boxes
'
),
mask_type
assert
obmask
.
is_mask
()
assert
obmask
.
chroma
==
1
assert
obmask
.
shape_dict
[
'
Z
'
]
==
1
lamap
=
label
(
obmask
.
data
[:,
:,
0
,
0
])
assert
obmask
.
nz
==
1
assert
zstack
.
hw
==
obmask
.
hw
# build object query
# assign object labels and build object query
lamap
=
label
(
obmask
.
data
[:,
:,
0
,
0
])
query_str
=
'
label > 0
'
# always true
if
filters
is
not
None
:
for
k
in
filters
.
keys
():
...
...
@@ -38,7 +51,7 @@ def build_zmask_from_object_mask(
query_str
=
query_str
+
f
'
&
{
k
}
>
{
vmin
}
&
{
k
}
<
{
vmax
}
'
# build dataframe of objects, assign z index to each object
argmax
=
stack
.
data
.
argmax
(
axis
=
3
,
keepdims
=
True
)[:,
:,
0
,
0
]
argmax
=
z
stack
.
data
.
argmax
(
axis
=
3
,
keepdims
=
True
)[:,
:,
0
,
0
]
df
=
(
pd
.
DataFrame
(
regionprops_table
(
...
...
@@ -63,11 +76,11 @@ def build_zmask_from_object_mask(
lut
=
np
.
zeros
(
lamap
.
max
()
+
1
)
-
1
lut
[
df
.
label
]
=
df
.
zi
# convert bounding boxes to
slice
s
# convert bounding boxes to
numpy slice object
s
ebxy
,
ebz
=
expand_box_by
h
,
w
,
c
,
nz
=
stack
.
shape
h
,
w
,
c
,
nz
=
z
stack
.
shape
boxes
=
[]
meta
=
[]
for
ob
in
df
.
itertuples
(
name
=
'
LabeledObject
'
):
y0
=
max
(
ob
.
y0
-
ebxy
,
0
)
y1
=
min
(
ob
.
y1
+
ebxy
,
h
-
1
)
...
...
@@ -84,7 +97,6 @@ def build_zmask_from_object_mask(
'
x1
'
:
ob
.
x1
-
x0
,
}
# sl = np.s_[z0: z1 + 1, y0: y1, x0: x1]
sl
=
np
.
s_
[
y0
:
y1
,
x0
:
x1
,
0
,
z0
:
z1
+
1
]
# compute contours
...
...
@@ -92,7 +104,7 @@ def build_zmask_from_object_mask(
contour
=
find_contours
(
obmask
)
mask
=
obmask
[
ob
.
y0
:
ob
.
y1
,
ob
.
x0
:
ob
.
x1
]
boxes
.
append
({
meta
.
append
({
'
info
'
:
ob
,
'
slice
'
:
sl
,
'
relative_bounding_box
'
:
rbb
,
...
...
@@ -101,7 +113,7 @@ def build_zmask_from_object_mask(
})
# build mask z-stack
zi_st
=
np
.
zeros
(
stack
.
shape
,
dtype
=
'
bool
'
)
zi_st
=
np
.
zeros
(
z
stack
.
shape
,
dtype
=
'
bool
'
)
if
mask_type
==
'
contours
'
:
zi_map
=
(
lut
[
lamap
]
+
1.0
).
astype
(
'
int
'
)
idxs
=
np
.
array
(
zi_map
)
-
1
...
...
@@ -116,8 +128,8 @@ def build_zmask_from_object_mask(
zi_st
[:,
:,
:,
-
1
][
lamap
==
0
]
=
0
elif
mask_type
==
'
boxes
'
:
for
bb
in
boxes
:
for
bb
in
meta
:
sl
=
bb
[
'
slice
'
]
zi_st
[
sl
]
=
1
return
zi_st
,
boxes
\ No newline at end of file
return
zi_st
,
meta
\ No newline at end of file
This diff is collapsed.
Click to expand it.
model_server/accessors.py
+
16
−
0
View file @
9942e931
...
...
@@ -39,6 +39,22 @@ class GenericImageDataAccessor(ABC):
c
=
int
(
channel
)
return
InMemoryDataAccessor
(
self
.
data
[:,
:,
c
:(
c
+
1
),
:])
@property
def
dtype
(
self
):
return
self
.
data
.
dtype
@property
def
hw
(
self
):
"""
Get data height and width as a tuple
:return: tuple of (Y, X) dimensions
"""
return
self
.
shape_dict
[
'
Y
'
],
self
.
shape_dict
[
'
X
'
]
@property
def
nz
(
self
):
return
self
.
shape_dict
[
'
Z
'
]
@property
def
data
(
self
):
"""
...
...
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