/
niftihandling.jl
151 lines (125 loc) · 4.41 KB
/
niftihandling.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
readphase(filename; rescale=true, keyargs...)
Reads the NIfTI phase with sanity checking and optional rescaling to [-π;π].
# Examples
```julia-repl
julia> phase = readphase("phase.nii")
```
### Optional keyargs are forwarded to `niread`:
$(@doc niread)
"""
function readphase(filename; rescale=true, keyargs...)
phase = niread(filename; keyargs...)
if phase.header.scl_slope == 0 # slope of 0 is always wrong
phase.header.scl_slope = 1
end
if rescale
minp, maxp = Float32.(approxextrema(phase))
if isapprox(maxp - minp, 2π; atol=0.1) # no rescaling required
return phase
end
minp, maxp = Float32.(approxextrema(phase.raw))
if isapprox(maxp - minp, 2π; atol=0.1) # no rescaling required, but header wrong
phase.header.scl_slope = 1
phase.header.scl_inter = 0
else # rescaling
phase.header.scl_slope = 2pi / (maxp - minp)
phase.header.scl_inter = -pi - minp * phase.header.scl_slope
end
end
return phase
end
"""
readmag(filename; rescale=false, keyargs...)
Reads the NIfTI magnitude with sanity checking and optional rescaling to [0;1].
# Examples
```julia-repl
julia> mag = readmag("mag.nii")
```
### Optional keyargs are forwarded to `niread`:
$(@doc niread)
"""
function readmag(fn; rescale=false, keyargs...)
mag = niread(fn; keyargs...)
if mag.header.scl_slope == 0
mag.header.scl_slope = 1
end
if rescale
mini, maxi = Float32.(approxextrema(mag.raw))
mag.header.scl_slope = 1 / (maxi - mini)
mag.header.scl_inter = - mini * mag.header.scl_slope
end
return mag
end
Base.copy(x::NIfTI.NIfTI1Header) = NIfTI.NIfTI1Header([getfield(x, k) for k ∈ fieldnames(NIfTI.NIfTI1Header)]...)
function Base.similar(header::NIfTI.NIfTI1Header)
hdr = copy(header)
hdr.scl_inter = 0
hdr.scl_slope = 1
return hdr
end
"""
header(v::NIVolume)
Returns a copy of the header with the orientation information.
# Examples
```julia-repl
julia> vol = readmag("image.nii")
julia> hdr = header(vol)
julia> savenii(vol .+ 10, "vol10.nii"; header=hdr)
```
"""
header(v::NIVolume) = similar(v.header)
function savenii(image, name, writedir, header=nothing)
if isnothing(writedir) return end
if !(last(splitext(name)) in [".nii", ".gz"])
name = "$name.nii"
end
savenii(image, joinpath(writedir, name); header)
end
"""
savenii(image::AbstractArray, filepath; header=nothing)
savenii(image::AbstractArray, name, writedir, header=nothing)
Warning: MRIcro can only open images with types Int32, Int64, Float32, Float64
# Examples
```julia-repl
julia> savenii(ones(64,64,5), "image.nii")
julia> savenii(ones(64,64,5), "image2", "folder")
```
"""
function savenii(image::AbstractArray, filepath; header=nothing)
vol = NIVolume([h for h in [header] if h !== nothing]..., image)
niwrite(filepath, vol)
return filepath
end
ConvertTypes = Union{BitArray, AbstractArray{UInt8}} #TODO debug NIfTI
MriResearchTools.savenii(image::ConvertTypes, args...;kwargs...) = savenii(Float32.(image), args...;kwargs...)
"""
write_emptynii(size, path; datatype=Float32, header=NIVolume(zeros(datatype, 1)).header)
Writes an empty NIfTI image to disk that can be used for memory-mapped access.
# Examples
```julia-repl
julia> vol = write_emptynii((64,64,64), "empty.nii")
julia> vol.raw[:,:,1] .= ones(64,64) # synchronizes mmapped file on disk
```
Warning: MRIcro can only open images with types Int32, Int64, Float32, Float64
"""
function write_emptynii(sz, path; datatype=Float32, header=NIVolume(zeros(datatype, 1)).header)
header = copy(header)
header.dim = Int16.((length(sz), sz..., ones(8-1-length(sz))...))
header.datatype = NIfTI.eltype_to_int16(datatype)
header.bitpix = NIfTI.nibitpix(datatype)
if isfile(path) rm(path) end
open(path, "w") do file
write(file, header)
write(file, Int32(0)) # offset of 4 bytes
end
return niread(path; mmap=true, mode="r+")
end
mmtovoxel(sizemm, nii::NIVolume) = mmtovoxel(sizemm, nii.header)
mmtovoxel(sizemm, header::NIfTI.NIfTI1Header) = mmtovoxel(sizemm, header.pixdim)
mmtovoxel(sizemm, pixdim) = sizemm ./ pixdim
getcomplex(mag::NIVolume, phase::NIVolume) = getcomplex(mag.raw, phase.raw)
function Base.setindex!(vol::NIVolume{<:AbstractFloat}, v, i...)
scaled = v / vol.header.scl_slope + vol.header.scl_inter
setindex!(vol.raw, scaled, i...)
end