Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
MPBA Radiomics
RADLER
Commits
2fa3bde1
Commit
2fa3bde1
authored
Mar 16, 2020
by
Alessia Marcolini
Browse files
Import and use new
parent
90f38f72
Changes
1
Hide whitespace changes
Inline
Side-by-side
02_radiomics_features_extraction/extract_features_radiomics.py
View file @
2fa3bde1
#%%
import
os
from
radiomics
import
featuree
xtractor
from
py
radiomics
_radler
import
MyRadiomicsFeaturesExtractor
as
RadiomicsFeatureE
xtractor
import
SimpleITK
as
sitk
import
numpy
as
np
import
pandas
as
pd
import
os
from
pathlib
import
Path
from
tqdm
import
tqdm
...
...
@@ -14,6 +13,8 @@ import warnings
warnings
.
filterwarnings
(
"ignore"
)
# %%
# os.chdir('..')
DATASET
=
Path
(
'HN_val'
)
BBOX
=
'bbox_64'
VOXEL_SPACING
=
(
1.0
,
1.0
,
1.0
)
...
...
@@ -25,30 +26,23 @@ PIXEL_SPACING = [1.0, 2.0, 3.0, 4.0, 5.0]
#%%
DATADIR
=
Path
(
'data'
)
/
DATASET
/
'processed'
/
'bbox'
/
BBOX
OUTDIR
=
Path
(
'data'
)
/
DATASET
/
'processed'
OUTFILE
=
f
'radiomics_features_
{
BBOX
}
.csv'
# output file name
OUTFILE
=
f
'radiomics_features_
{
BBOX
}
_radnew21.csv'
# output file name
clinical
=
pd
.
read_csv
(
Path
(
'data'
)
/
DATASET
/
'processed'
/
f
'clinical_
{
DATASET
}
.csv'
)
#%%
params_shape
=
'shape.yaml'
# param file to use to create the extractor
extractor_shape
=
featureextractor
.
RadiomicsFeature
s
Extractor
(
params_shape
)
params_shape
=
'
02_radiomics_features_extraction/
shape
_new
.yaml'
# param file to use to create the extractor
extractor_shape
=
RadiomicsFeatureExtractor
(
params_shape
)
params_intensity
=
'intensity.yaml'
# param file to use to create the extractor
extractor_intensity
=
featureextractor
.
RadiomicsFeature
s
Extractor
(
params_intensity
)
params_intensity
=
'
02_radiomics_features_extraction/
intensity.yaml'
# param file to use to create the extractor
extractor_intensity
=
RadiomicsFeatureExtractor
(
params_intensity
)
params_texture
=
'texture.yaml'
# param file to use to create the extractor
extractor_texture
=
featureextractor
.
RadiomicsFeature
s
Extractor
(
params_texture
)
params_texture
=
'
02_radiomics_features_extraction/
texture
_new
.yaml'
# param file to use to create the extractor
extractor_texture
=
RadiomicsFeatureExtractor
(
params_texture
)
filenames
=
[
f
for
f
in
os
.
listdir
(
DATADIR
)
if
f
.
endswith
(
'.npy'
)]
filenames
=
[
f
for
f
in
os
.
listdir
(
DATADIR
)
if
f
.
endswith
(
'.npy'
)]
[:
2
]
exclude_list
=
[]
# 'HN-CHUM-011',
# 'HN-HMR-008',
# 'HN-CHUS-043',
# 'HN-HMR-002',
# 'HN-CHUM-010',
# 'HN-HGJ-044',
# ] #'HN-CHUM-020', 'HN-CHUS-093', 'HN-HMR-001', 'HN-HMR-007', 'HN-HMR-017'] # BAD SUV conversion
#%%
def
process_file
(
filename
):
...
...
@@ -66,12 +60,15 @@ def process_file(filename):
curr_modality
.
SetSpacing
(
VOXEL_SPACING
)
modalities
.
append
(
curr_modality
)
# print(data[-1, :, :, :] == data[1, :, :, :])
# print(np.max(data[-1, :, :, :]))
has_label
=
np
.
max
(
data
[
-
1
,
:,
:,
:])
>
0
if
has_label
:
segmentation
=
sitk
.
GetImageFromArray
(
data
[
-
1
,
:,
:,
:].
astype
(
np
.
uint8
))
segmentation
.
SetSpacing
(
VOXEL_SPACING
)
feature
=
{}
# shape features
result
=
extractor_shape
.
execute
(
modalities
[
0
],
segmentation
)
for
key
,
value
in
result
.
items
():
...
...
@@ -95,28 +92,26 @@ def process_file(filename):
# uniform binning
extractor_texture
.
settings
[
'binMode'
]
=
'uniform'
try
:
result
=
extractor_texture
.
execute
(
modality
,
segmentation
)
for
key
,
value
in
result
.
items
():
if
not
key
.
startswith
(
"general_"
):
feature
[
f
'
{
key
}
_
{
P
}
_
{
NB
}
_uniform_
{
name
}
'
]
=
result
[
key
]
# equalized binning
extractor_texture
.
settings
[
'binMode'
]
=
'equal'
result
=
extractor_texture
.
execute
(
modality
,
segmentation
)
for
key
,
value
in
result
.
items
():
if
not
key
.
startswith
(
"general_"
):
feature
[
f
'
{
key
}
_
{
P
}
_
{
NB
}
_equalized_
{
name
}
'
]
=
result
[
key
]
except
:
broken
=
True
result
=
extractor_texture
.
execute
(
modality
,
segmentation
)
for
key
,
value
in
result
.
items
():
if
not
key
.
startswith
(
"general_"
):
feature
[
f
'
{
key
}
_
{
P
}
_
{
NB
}
_uniform_
{
name
}
'
]
=
result
[
key
]
# equalized binning
extractor_texture
.
settings
[
'binMode'
]
=
'equal'
result
=
extractor_texture
.
execute
(
modality
,
segmentation
)
for
key
,
value
in
result
.
items
():
if
not
key
.
startswith
(
"general_"
):
feature
[
f
'
{
key
}
_
{
P
}
_
{
NB
}
_equalized_
{
name
}
'
]
=
result
[
key
]
if
not
broken
:
feature
[
'filename'
]
=
filename
...
...
@@ -127,22 +122,32 @@ def process_file(filename):
return
feature
else
:
return
{}
return
{
'filename'
:
filename
,
'patient'
:
patient
}
#%% MULTIPROCESS
result
=
[]
for
file
in
filenames
:
feat
=
process_file
(
file
)
result
.
append
(
feat
)
multiproc
=
ListMultiprocessing
(
process_file
,
N_JOBS
)
multiproc
.
execute
(
filenames
)
result
=
multiproc
.
get_result
()
print
(
'done'
)
# %%
# multiproc = ListMultiprocessing(process_file, N_JOBS)
# multiproc.execute(filenames)
# result = multiproc.get_result()
# print('done')
#%% Save features in a Pandas df
features
=
pd
.
DataFrame
(
result
)
features
.
set_index
(
'filename'
,
inplace
=
True
)
print
(
'Number of files: '
,
features
.
shape
[
0
])
print
(
'Number of features: '
,
features
.
shape
[
1
])
print
(
'Number of features: '
,
features
.
shape
[
1
]
-
1
)
# %%
features
.
to_csv
(
OUTDIR
/
OUTFILE
)
# %%
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment