Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SVLT
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
ALMF
SVLT
Commits
9fe25e61
Commit
9fe25e61
authored
1 year ago
by
Christopher Randolph Rhodes
Browse files
Options
Downloads
Patches
Plain Diff
Merged in image process functions and padding tests
parent
5919b052
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
model_server/process.py
+29
-21
29 additions, 21 deletions
model_server/process.py
tests/test_process.py
+30
-0
30 additions, 0 deletions
tests/test_process.py
with
59 additions
and
21 deletions
model_server/process.py
+
29
−
21
View file @
9fe25e61
...
...
@@ -6,38 +6,40 @@ from math import ceil, floor
import
numpy
as
np
from
skimage.exposure
import
rescale_intensity
def
pad
(
im
,
mpx
):
# now in model_server.batch
'''
Pads and crops image width edge values to specified dimension
'''
dh
=
0.5
*
(
mpx
-
im
.
shape
[
0
])
dw
=
0.5
*
(
mpx
-
im
.
shape
[
1
])
def
pad
(
yxcz
,
mpx
:
int
):
"""
Pad and crop image data in Y and X axes to meet specific dimension
:param yxcz: np.ndarray
:param mpx: int pixel size of resulting square
:return: np.ndarray array of size (mpx, mpx, nc, nz)
"""
assert
len
(
yxcz
.
shape
)
==
4
dh
=
0.5
*
(
mpx
-
yxcz
.
shape
[
0
])
dw
=
0.5
*
(
mpx
-
yxcz
.
shape
[
1
])
if
dw
<
0
:
x0
=
floor
(
-
dw
)
x1
=
x0
+
mpx
im
=
im
[:,
x0
:
x1
]
yxcz
=
yxcz
[:,
x0
:
x1
,
:,
:
]
dw
=
0
if
dh
<
0
:
y0
=
floor
(
-
dh
)
y1
=
y0
+
mpx
im
=
im
[
y0
:
y1
,
:]
yxcz
=
yxcz
[
y0
:
y1
,
:,
:
,
:]
dh
=
0
border
=
((
floor
(
dh
),
ceil
(
dh
)),
(
floor
(
dw
),
ceil
(
dw
)))
padded
=
np
.
pad
(
im
,
border
,
mode
=
'
constant
'
)
if
padded
.
shape
!=
(
mpx
,
mpx
):
raise
Exception
(
f
'
Incorrect image shape:
{
padded
.
shape
}
v.
{
(
mpx
,
mpx
)
}
'
)
return
padded
def
pad_3d
(
im
,
mpx
):
# im: [z x h x w]
assert
(
len
(
im
.
shape
)
==
3
)
nz
,
h
,
w
=
im
.
shape
padded
=
np
.
zeros
((
nz
,
mpx
,
mpx
),
dtype
=
im
.
dtype
)
for
zi
in
range
(
nz
):
padded
[
zi
,
:,
:]
=
pad
(
im
[
zi
,
:,
:],
mpx
)
border
=
((
floor
(
dh
),
ceil
(
dh
)),
(
floor
(
dw
),
ceil
(
dw
)),
(
0
,
0
),
(
0
,
0
))
padded
=
np
.
pad
(
yxcz
,
border
,
mode
=
'
constant
'
)
return
padded
def
resample
(
nda
,
cmin
=
0
,
cmax
=
2
**
16
):
# now in model_server.batch
def
resample_to_8bit
(
nda
,
cmin
=
0
,
cmax
=
2
**
16
):
"""
Resample a 16 bit image to 8 bit, optionally bracketing a given intensity range
:param nda: np.ndarray input data of arbitrary dimension
:param cmin: intensity level on 16-bit scale that become zero in 8-bit scale
:param cmax: intensity level on 16-bit scale that become maximum (255) in 8-bit scale
:return: rescaled data of same dimension as input
"""
return
rescale_intensity
(
np
.
clip
(
nda
,
cmin
,
cmax
),
in_range
=
(
cmin
,
cmax
+
1
),
...
...
@@ -45,7 +47,13 @@ def resample(nda, cmin=0, cmax=2**16): # now in model_server.batch
).
astype
(
'
uint8
'
)
def
rescale
(
nda
,
clip
=
0.0
):
# now in model_server.batch
def
rescale
(
nda
,
clip
=
0.0
):
"""
Rescale an image for a given clipping ratio
:param nda: input data of arbitrary dimension and scale
:param clip: Ratio of clipping in the resulting image
:return: rescaled image of same dimension as input
"""
clip_pct
=
(
100.0
*
clip
,
100.0
*
(
1.0
-
clip
))
cmin
,
cmax
=
np
.
percentile
(
nda
,
clip_pct
)
rescaled
=
rescale_intensity
(
nda
,
in_range
=
(
cmin
,
cmax
))
...
...
This diff is collapsed.
Click to expand it.
tests/test_process.py
0 → 100644
+
30
−
0
View file @
9fe25e61
import
unittest
import
numpy
as
np
from
model_server.process
import
pad
class
TestProcessingUtilityMethods
(
unittest
.
TestCase
):
def
setUp
(
self
)
->
None
:
w
=
200
h
=
300
nc
=
4
nz
=
11
self
.
data2d
=
(
2
**
16
*
np
.
random
.
rand
(
h
,
w
,
1
,
1
)).
astype
(
'
uint16
'
)
self
.
data3d
=
(
2
**
16
*
np
.
random
.
rand
(
h
,
w
,
1
,
nz
)).
astype
(
'
uint16
'
)
self
.
data4d
=
(
2
**
16
*
np
.
random
.
rand
(
h
,
w
,
nc
,
nz
)).
astype
(
'
uint16
'
)
def
test_pad_2d
(
self
):
padded
=
pad
(
self
.
data2d
,
256
)
self
.
assertEqual
(
padded
.
shape
,
(
256
,
256
,
1
,
1
))
def
test_pad_3d
(
self
):
nz
=
self
.
data3d
.
shape
[
3
]
padded
=
pad
(
self
.
data3d
,
256
)
self
.
assertEqual
(
padded
.
shape
,
(
256
,
256
,
1
,
nz
))
def
test_pad_4d
(
self
):
nc
=
self
.
data4d
.
shape
[
2
]
nz
=
self
.
data4d
.
shape
[
3
]
padded
=
pad
(
self
.
data4d
,
256
)
self
.
assertEqual
(
padded
.
shape
,
(
256
,
256
,
nc
,
nz
))
\ No newline at end of file
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