Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hyunji
/
A-Performance-Evaluation-of-CNN-for-Brain-Age-Prediction-Using-Structural-MRI-Data
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Hyunji
2021-12-20 04:26:26 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
745d7f9297e8504668695df81d0c20bdfc05f0cf
745d7f92
1 parent
07024ed1
data utils
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
0 deletions
2DCNN/src/common/data_utils.py
2DCNN/src/common/data_utils.py
0 → 100644
View file @
745d7f9
import
numpy
def
find_closest
(
value
,
array
):
array
=
numpy
.
asarray
(
array
)
idx
=
(
numpy
.
abs
(
array
-
value
))
.
argmin
()
return
array
[
idx
]
def
frame_drop
(
scan
,
frame_keep_style
=
"random"
,
frame_keep_fraction
=
1
,
frame_dim
=
1
,
impute
=
"drop"
):
if
frame_keep_fraction
>=
1
:
return
scan
n
=
scan
.
shape
[
frame_dim
]
if
frame_keep_style
==
"random"
:
frames_to_keep
=
int
(
numpy
.
floor
(
n
*
frame_keep_fraction
))
indices
=
numpy
.
random
.
permutation
(
numpy
.
arange
(
n
))[:
frames_to_keep
]
elif
frame_keep_style
==
"ordered"
:
k
=
int
(
numpy
.
ceil
(
1
/
frame_keep_fraction
))
indices
=
numpy
.
arange
(
0
,
n
,
k
)
# pick every k'th frame
else
:
raise
Exception
(
"Wrong frame drop style"
)
if
impute
==
"zeros"
:
if
frame_dim
==
1
:
t
=
numpy
.
zeros
((
1
,
n
,
1
,
1
))
t
[:,
indices
]
=
1
return
scan
*
t
if
frame_dim
==
2
:
t
=
numpy
.
zeros
((
1
,
1
,
n
,
1
))
t
[:,
:,
indices
]
=
1
return
scan
*
t
if
frame_dim
==
3
:
t
=
numpy
.
zeros
((
1
,
1
,
1
,
n
))
t
[:,
:,
:,
indices
]
=
1
return
scan
*
t
elif
impute
==
"fill"
:
# fill with nearest available frame
if
frame_dim
==
1
:
for
i
in
range
(
scan
.
shape
[
1
]):
if
i
in
indices
:
pass
scan
[:,
i
,
:,
:]
=
scan
[:,
find_closest
(
i
,
indices
),
:,
:]
return
scan
if
frame_dim
==
2
:
for
i
in
range
(
scan
.
shape
[
1
]):
if
i
in
indices
:
pass
scan
[:,
:,
i
,
:]
=
scan
[:,
:,
find_closest
(
i
,
indices
),
:]
return
scan
if
frame_dim
==
3
:
for
i
in
range
(
scan
.
shape
[
1
]):
if
i
in
indices
:
pass
scan
[:,
:,
:,
i
]
=
scan
[:,
:,
:,
find_closest
(
i
,
indices
)]
return
scan
elif
impute
==
"noise"
:
noise
=
numpy
.
random
.
uniform
(
high
=
scan
.
max
(),
low
=
scan
.
min
(),
size
=
scan
.
shape
)
if
frame_dim
==
1
:
t
=
numpy
.
zeros
((
1
,
n
,
1
,
1
))
t
[:,
indices
]
=
1
return
scan
+
noise
*
(
1
-
t
)
if
frame_dim
==
2
:
t
=
numpy
.
zeros
((
1
,
1
,
n
,
1
))
t
[:,
:,
indices
]
=
1
return
scan
+
noise
*
(
1
-
t
)
if
frame_dim
==
3
:
t
=
numpy
.
zeros
((
1
,
1
,
1
,
n
))
t
[:,
:,
:,
indices
]
=
1
return
scan
+
noise
*
(
1
-
t
)
else
:
# drop
if
frame_dim
==
1
:
return
scan
[:,
indices
,
:,
:]
if
frame_dim
==
2
:
return
scan
[:,
:,
indices
,
:]
if
frame_dim
==
3
:
return
scan
[:,
:,
:,
indices
]
return
scan
def
gaussian_noise
(
scan
,
sigma
=
0.0
):
return
scan
+
sigma
*
numpy
.
random
.
randn
(
*
scan
.
shape
)
def
intensity_scaling
(
scan
):
scale
=
2
**
(
2
*
numpy
.
random
.
rand
()
-
1
)
# 2**(-1,1)
return
scan
*
scale
Please
register
or
login
to post a comment