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
109b10f2
Commit
109b10f2
authored
1 year ago
by
pswain
Browse files
Options
Downloads
Patches
Plain Diff
renamed variables in extractor
parent
85a66bb2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/extraction/core/extractor.py
+35
-33
35 additions, 33 deletions
src/extraction/core/extractor.py
src/extraction/core/functions/loaders.py
+12
-10
12 additions, 10 deletions
src/extraction/core/functions/loaders.py
with
47 additions
and
43 deletions
src/extraction/core/extractor.py
+
35
−
33
View file @
109b10f2
...
...
@@ -30,7 +30,7 @@ extraction_result = t.Dict[
# or their background. These global variables both allow the functions
# to be stored in a dictionary for access only on demand and to be
# defined simply in extraction/core/functions.
CELL_FUNS
,
TRAPFUNS
,
FUNS
=
load_funs
()
CELL_FUNS
,
TRAP
_
FUNS
,
ALL_
FUNS
=
load_funs
()
CUSTOM_FUNS
,
CUSTOM_ARGS
=
load_custom_args
()
RED_FUNS
=
load_redfuns
()
...
...
@@ -234,7 +234,7 @@ class Extractor(StepABC):
self
.
load_custom_funs
()
self
.
_all_cell_funs
=
set
(
self
.
_custom_funs
.
keys
()).
union
(
CELL_FUNS
)
# merge the two dicts
self
.
_all_funs
=
{
**
self
.
_custom_funs
,
**
FUNS
}
self
.
_all_funs
=
{
**
self
.
_custom_funs
,
**
ALL_
FUNS
}
def
load_meta
(
self
):
"""
Load metadata from h5 file.
"""
...
...
@@ -289,7 +289,7 @@ class Extractor(StepABC):
self
,
traps
:
t
.
List
[
np
.
ndarray
],
masks
:
t
.
List
[
np
.
ndarray
],
metric
:
str
,
cell_property
:
str
,
cell_labels
:
t
.
Dict
[
int
,
t
.
List
[
int
]],
)
->
t
.
Tuple
[
t
.
Union
[
t
.
Tuple
[
float
],
t
.
Tuple
[
t
.
Tuple
[
int
]]]]:
"""
...
...
@@ -301,8 +301,8 @@ class Extractor(StepABC):
t.List of images.
masks: list of arrays
t.List of masks.
metric
: str
Metric
to extract.
cell_property
: str
Property
to extract
, including imBackground
.
cell_labels: dict
A dict of cell labels with trap_ids as keys and a list
of cell labels as values.
...
...
@@ -317,21 +317,21 @@ class Extractor(StepABC):
"""
if
cell_labels
is
None
:
self
.
_log
(
"
No cell labels given. Sorting cells using index.
"
)
cell_fun
=
True
if
metric
in
self
.
_all_cell_funs
else
False
cell_fun
=
True
if
cell_property
in
self
.
_all_cell_funs
else
False
idx
=
[]
results
=
[]
for
trap_id
,
(
mask_set
,
trap
,
l
bl_set
)
in
enumerate
(
for
trap_id
,
(
mask_set
,
trap
,
l
ocal_cell_labels
)
in
enumerate
(
zip
(
masks
,
traps
,
cell_labels
.
values
())
):
# ignore empty traps
if
len
(
mask_set
):
#
apply metric either a cell function or otherwis
e
result
=
self
.
_all_funs
[
metric
](
mask_set
,
trap
)
#
find property from the til
e
result
=
self
.
_all_funs
[
cell_property
](
mask_set
,
trap
)
if
cell_fun
:
# store results for each cell separately
for
lb
l
,
val
in
zip
(
l
bl_set
,
result
):
for
cell_labe
l
,
val
in
zip
(
l
ocal_cell_labels
,
result
):
results
.
append
(
val
)
idx
.
append
((
trap_id
,
lb
l
))
idx
.
append
((
trap_id
,
cell_labe
l
))
else
:
# background (trap) function
results
.
append
(
result
)
...
...
@@ -343,19 +343,19 @@ class Extractor(StepABC):
self
,
traps
:
t
.
List
[
np
.
array
],
masks
:
t
.
List
[
np
.
array
],
metric
s
:
t
.
List
[
str
],
cell_propertie
s
:
t
.
List
[
str
],
**
kwargs
,
)
->
t
.
Dict
[
str
,
pd
.
Series
]:
"""
Return dict with metrics as key and
metrics applied to data
as values.
Return dict with metrics as key and
cell_properties
as values.
Data from one time point is used.
"""
d
=
{
metric
:
self
.
extract_traps
(
traps
=
traps
,
masks
=
masks
,
metric
=
metric
,
**
kwargs
cell_property
:
self
.
extract_traps
(
traps
=
traps
,
masks
=
masks
,
cell_property
=
cell_property
,
**
kwargs
)
for
metric
in
metric
s
for
cell_property
in
cell_propertie
s
}
return
d
...
...
@@ -363,7 +363,7 @@ class Extractor(StepABC):
self
,
traps
:
np
.
ndarray
,
masks
:
t
.
List
[
np
.
ndarray
],
re
d_metrics
:
t
.
Dict
[
reduction_method
,
t
.
Collection
[
str
]],
t
re
e_branch
:
t
.
Dict
[
reduction_method
,
t
.
Collection
[
str
]],
**
kwargs
,
)
->
t
.
Dict
[
str
,
t
.
Dict
[
reduction_method
,
t
.
Dict
[
str
,
pd
.
Series
]]]:
"""
...
...
@@ -375,10 +375,10 @@ class Extractor(StepABC):
An array of image data arranged as (tiles, X, Y, Z)
masks: list of arrays
An array of masks for each trap: one per cell at the trap
re
d_metrics
: dict
dict for which keys are reduction functions and values are
either a list or a set of strings giving the metric
functions
.
t
re
e_branch
: dict
An upper branch of the extraction tree: a dict for which
keys are reduction functions and values are either a list
or a set of strings giving the cell properties to be found
.
For example: {
'
np_max
'
: {
'
max5px
'
,
'
mean
'
,
'
median
'
}}
**kwargs: dict
All other arguments passed to Extractor.extract_funs.
...
...
@@ -392,21 +392,22 @@ class Extractor(StepABC):
kwargs
[
"
cell_labels
"
]
=
kwargs
.
pop
(
"
labels
"
)
# create dict with keys naming the reduction in the z-direction
# and the reduced data as values
reduced_tiles
_data
=
{}
reduced_tiles
=
{}
if
traps
is
not
None
:
for
red_fun
in
re
d_metrics
.
keys
():
reduced_tiles
_data
[
red_fun
]
=
[
for
red_fun
in
t
re
e_branch
.
keys
():
reduced_tiles
[
red_fun
]
=
[
self
.
reduce_dims
(
tile_data
,
method
=
RED_FUNS
[
red_fun
])
for
tile_data
in
traps
]
# calculate cell and tile properties
d
=
{
red_fun
:
self
.
extract_funs
(
metrics
=
metric
s
,
traps
=
reduced_tiles
_data
.
get
(
red_fun
,
[
None
for
_
in
masks
]),
cell_properties
=
cell_propertie
s
,
traps
=
reduced_tiles
.
get
(
red_fun
,
[
None
for
_
in
masks
]),
masks
=
masks
,
**
kwargs
,
)
for
red_fun
,
metric
s
in
re
d_metrics
.
items
()
for
red_fun
,
cell_propertie
s
in
t
re
e_branch
.
items
()
}
return
d
...
...
@@ -505,7 +506,7 @@ class Extractor(StepABC):
"""
d
=
{}
img_bgsub
=
{}
for
ch
,
re
d_metrics
in
tree_bits
[
"
tree
"
].
items
():
for
ch
,
t
re
e_branch
in
tree_bits
[
"
tree
"
].
items
():
# NB ch != is necessary for threading
if
ch
!=
"
general
"
and
tiles
is
not
None
and
len
(
tiles
):
# image data for all traps for a particular channel and time point
...
...
@@ -513,12 +514,13 @@ class Extractor(StepABC):
# we use 0 here to access the single time point available
img
=
tiles
[:,
tree_bits
[
"
tree_channels
"
].
index
(
ch
),
0
]
else
:
# no reduction applied to bright-field images
img
=
None
# apply metrics to image data
d
[
ch
]
=
self
.
reduce_extract
(
traps
=
img
,
masks
=
masks
,
re
d_metrics
=
red_metrics
,
t
re
e_branch
=
tree_branch
,
cell_labels
=
cell_labels
,
**
kwargs
,
)
...
...
@@ -528,20 +530,20 @@ class Extractor(StepABC):
ch_bs
=
ch
+
"
_bgsub
"
# subtract median background
bgsub_mapping
=
map
(
# move
time
to last column to allow subtraction
# move
Z
to last column to allow subtraction
lambda
img
,
bgs
:
np
.
moveaxis
(
img
,
0
,
-
1
)
# median of background over all pixels for each
time point
# median of background over all pixels for each
Z section
-
bn
.
median
(
img
[:,
bgs
],
axis
=
1
),
img
,
bgs
,
)
# apply map and convert to array
mapping_result
=
np
.
stack
(
list
(
bgsub_mapping
))
# move
time
axis back to the second column
# move
Z
axis back to the second column
img_bgsub
[
ch_bs
]
=
np
.
moveaxis
(
mapping_result
,
-
1
,
1
)
# apply metrics to background-corrected data
d
[
ch_bs
]
=
self
.
reduce_extract
(
re
d_metrics
=
tree_bits
[
"
channel_tree
"
][
ch
],
t
re
e_branch
=
tree_bits
[
"
channel_tree
"
][
ch
],
traps
=
img_bgsub
[
ch_bs
],
masks
=
masks
,
cell_labels
=
cell_labels
,
...
...
This diff is collapsed.
Click to expand it.
src/extraction/core/functions/loaders.py
+
12
−
10
View file @
109b10f2
...
...
@@ -11,8 +11,10 @@ from extraction.core.functions.math_utils import div0
"""
Load functions for analysing cells and their background.
Note that inspect.getmembers returns a list of function names and
functions, and inspect.getfullargspec returns a function
'
s arguments.
Note that inspect.getmembers returns a list of function names
and functions, and inspect.getfullargspec returns a
function
'
s arguments.
"""
...
...
@@ -66,7 +68,7 @@ def load_cellfuns():
# create dict of the core functions from cell.py - these functions apply to a single mask
cell_funs
=
load_cellfuns_core
()
# create a dict of functions that apply the core functions to an array of cell_masks
CELLFUNS
=
{}
CELL
_
FUNS
=
{}
for
f_name
,
f
in
cell_funs
.
items
():
if
isfunction
(
f
):
...
...
@@ -79,27 +81,27 @@ def load_cellfuns():
# function that applies f to m and img, the trap_image
return
lambda
m
,
img
:
trap_apply
(
f
,
m
,
img
)
CELLFUNS
[
f_name
]
=
tmp
(
f
)
return
CELLFUNS
CELL
_
FUNS
[
f_name
]
=
tmp
(
f
)
return
CELL
_
FUNS
def
load_trapfuns
():
"""
Load functions that are applied to an entire tile.
"""
TRAPFUNS
=
{
TRAP
_
FUNS
=
{
f
[
0
]:
f
[
1
]
for
f
in
getmembers
(
trap
)
if
isfunction
(
f
[
1
])
and
f
[
1
].
__module__
.
startswith
(
"
extraction.core.functions
"
)
}
return
TRAPFUNS
return
TRAP
_
FUNS
def
load_funs
():
"""
Combine all automatically loaded functions.
"""
CELLFUNS
=
load_cellfuns
()
TRAPFUNS
=
load_trapfuns
()
CELL
_
FUNS
=
load_cellfuns
()
TRAP
_
FUNS
=
load_trapfuns
()
# return dict of cell funs, dict of trap funs, and dict of both
return
CELLFUNS
,
TRAPFUNS
,
{
**
TRAPFUNS
,
**
CELLFUNS
}
return
CELL
_
FUNS
,
TRAP
_
FUNS
,
{
**
TRAP
_
FUNS
,
**
CELL
_
FUNS
}
def
load_redfuns
(
...
...
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