Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mu-map
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tamino Huxohl
mu-map
Commits
e0d463db
Commit
e0d463db
authored
2 years ago
by
Tamino Huxohl
Browse files
Options
Downloads
Patches
Plain Diff
move function of data/dicom_util to file/dicom
parent
47350717
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
mu_map/data/dicom_util.py
+0
-50
0 additions, 50 deletions
mu_map/data/dicom_util.py
mu_map/data/prepare.py
+1
-1
1 addition, 1 deletion
mu_map/data/prepare.py
mu_map/file/dicom.py
+80
-5
80 additions, 5 deletions
mu_map/file/dicom.py
with
81 additions
and
56 deletions
mu_map/data/dicom_util.py
deleted
100644 → 0
+
0
−
50
View file @
47350717
from
datetime
import
datetime
from
enum
import
Enum
import
pydicom
class
DICOMTime
(
Enum
):
Study
=
1
Series
=
2
Acquisition
=
3
Content
=
4
def
date_field
(
self
):
return
f
"
{
self
.
name
}
Date
"
def
time_field
(
self
):
return
f
"
{
self
.
name
}
Time
"
def
to_datetime
(
self
,
dicom
:
pydicom
.
dataset
.
FileDataset
)
->
datetime
:
_date
=
dicom
[
self
.
date_field
()].
value
_time
=
dicom
[
self
.
time_field
()].
value
return
datetime
(
year
=
int
(
_date
[
0
:
4
]),
month
=
int
(
_date
[
4
:
6
]),
day
=
int
(
_date
[
6
:
8
]),
hour
=
int
(
_time
[
0
:
2
]),
minute
=
int
(
_time
[
2
:
4
]),
second
=
int
(
_time
[
4
:
6
]),
# microsecond=int(_time.split(".")[1]),
)
def
parse_age
(
patient_age
:
str
)
->
int
:
"""
Parse an age string as defined in the DICOM standard into an integer representing the age in years.
:param patient_age: age string as defined in the DICOM standard
:return: the age in years as a number
"""
assert
(
type
(
patient_age
)
==
str
),
f
"
patient age needs to be a string and not
{
type
(
patient_age
)
}
"
assert
(
len
(
patient_age
)
==
4
),
f
"
patient age [
{
patient_age
}
] has to be four characters long
"
_num
,
_format
=
patient_age
[:
3
],
patient_age
[
3
]
assert
(
_format
==
"
Y
"
),
f
"
currently, only patient ages in years [Y] is supported, not [
{
_format
}
]
"
return
int
(
_num
)
This diff is collapsed.
Click to expand it.
mu_map/data/prepare.py
+
1
−
1
View file @
e0d463db
...
...
@@ -8,7 +8,7 @@ import numpy as np
import
pandas
as
pd
import
pydicom
from
mu_map.
data
.dicom
_util
import
DICOMTime
,
parse_age
from
mu_map.
file
.dicom
import
DICOMTime
,
parse_age
from
mu_map.logging
import
add_logging_args
,
get_logger_by_args
...
...
This diff is collapsed.
Click to expand it.
mu_map/file/dicom.py
+
80
−
5
View file @
e0d463db
from
datetime
import
datetime
from
enum
import
Enum
import
random
from
typing
import
Tuple
...
...
@@ -29,6 +31,69 @@ Default extension of dicom files.
EXTENSION_DICOM
=
"
.dcm
"
class
DICOMTime
(
Enum
):
"""
Class for parsing dates and times defined in a DICOM header
into a python datetime type. It maps the four datetime fields
[Study, Series, Acquisition, Content] of the DICOM header into
an enumeration type.
Usage: DICOMTime.Series.to_datetime(dcm)
"""
Study
=
1
Series
=
2
Acquisition
=
3
Content
=
4
def
date_field
(
self
)
->
str
:
"""
Get the name of the date field according to this DICOMTime type.
"""
return
f
"
{
self
.
name
}
Date
"
def
time_field
(
self
)
->
str
:
"""
Get the name of the time field according to this DICOMTime type.
"""
return
f
"
{
self
.
name
}
Time
"
def
to_datetime
(
self
,
dicom
:
dcm_type
)
->
datetime
:
"""
Get the datetime according to this DICOMTime type.
"""
_date
=
dicom
[
self
.
date_field
()].
value
_time
=
dicom
[
self
.
time_field
()].
value
return
datetime
(
year
=
int
(
_date
[
0
:
4
]),
month
=
int
(
_date
[
4
:
6
]),
day
=
int
(
_date
[
6
:
8
]),
hour
=
int
(
_time
[
0
:
2
]),
minute
=
int
(
_time
[
2
:
4
]),
second
=
int
(
_time
[
4
:
6
]),
)
def
parse_age
(
patient_age
:
str
)
->
int
:
"""
Parse an age string as defined in the DICOM standard into an integer representing the age in years.
:param patient_age: age string as defined in the DICOM standard
:return: the age in years as a number
"""
assert
(
type
(
patient_age
)
==
str
),
f
"
patient age needs to be a string and not
{
type
(
patient_age
)
}
"
assert
(
len
(
patient_age
)
==
4
),
f
"
patient age [
{
patient_age
}
] has to be four characters long
"
_num
,
_format
=
patient_age
[:
3
],
patient_age
[
3
]
assert
(
_format
==
"
Y
"
),
f
"
currently, only patient ages in years [Y] is supported, not [
{
_format
}
]
"
return
int
(
_num
)
def
load_dcm
(
filename
:
str
)
->
Tuple
[
pydicom
.
dataset
.
FileDataset
,
np
.
ndarray
]:
"""
Load a DICOM image, the data as a numpy array and apply normalization of the Siemens SPECT/CT
...
...
@@ -69,7 +134,7 @@ def scale_image(image: np.ndarray, initial_scale=10000000) -> Tuple[np.ndarray,
is smaller than the maximum uint16 number
:return: the image scaled and converted to uint16 as well as the used scaling factor
"""
if
image
.
min
()
<
0.0
:
if
image
.
min
()
<
0.0
:
raise
ValueError
(
"
Cannot scale images with negative values!
"
)
scale
=
initial_scale
...
...
@@ -121,15 +186,25 @@ def change_uid(dcm: pydicom.dataset.FileDataset) -> pydicom.dataset.FileDataset:
:param dcm: the DICOM file to be udpated
:return: the DICOM file with updated UIDs
"""
dcm
.
SeriesInstanceUID
=
UID_PREFIX
+
str
(
random
.
randint
(
10000000000000
,
99999999999999
))
dcm
.
SOPInstanceUID
=
UID_PREFIX
+
str
(
random
.
randint
(
10000000000000
,
99999999999999
))
dcm
.
SeriesInstanceUID
=
UID_PREFIX
+
str
(
random
.
randint
(
10000000000000
,
99999999999999
)
)
dcm
.
SOPInstanceUID
=
UID_PREFIX
+
str
(
random
.
randint
(
10000000000000
,
99999999999999
)
)
return
dcm
if
__name__
==
"
__main__
"
:
import
argparse
parser
=
argparse
.
ArgumentParser
(
description
=
"
Dump the header of a DICOM image
"
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
"
file
"
,
type
=
str
,
help
=
"
the DICOM file of which the header is dumped
"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"
Dump the header of a DICOM image
"
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
,
)
parser
.
add_argument
(
"
file
"
,
type
=
str
,
help
=
"
the DICOM file of which the header is dumped
"
)
args
=
parser
.
parse_args
()
dcm
=
pydicom
.
dcmread
(
args
.
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