Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SNR calculation for single BG frame files #116

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/MDFCommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,32 @@
end

function systemMatrixWithBG(f::Union{MDFFileV2, MDFv2InMemory})
if !experimentHasMeasurement(f) || !measIsFastFrameAxis(f) || !measIsFourierTransformed(f)
if !experimentHasMeasurement(f) || !measIsFourierTransformed(f)
return nothing
end

data_ = measDataRaw(f)

if !measIsFastFrameAxis(f)
data_ = permutedims(data_, [4, 1, 2, 3])

Check warning on line 106 in src/MDFCommon.jl

View check run for this annotation

Codecov / codecov/patch

src/MDFCommon.jl#L106

Added line #L106 was not covered by tests
end

data = data_[:, :, :, :]
return data
end

# This is a special variant used for matrix compression
function systemMatrixWithBG(f::Union{MDFFileV2, MDFv2InMemory}, freq)
if !experimentHasMeasurement(f) || !measIsFastFrameAxis(f) || !measIsFourierTransformed(f)
if !experimentHasMeasurement(f) || !measIsFourierTransformed(f)
return nothing
end

data_ = measDataRaw(f)

if !measIsFastFrameAxis(f)
data_ = permutedims(data_, [4, 1, 2, 3])

Check warning on line 122 in src/MDFCommon.jl

View check run for this annotation

Codecov / codecov/patch

src/MDFCommon.jl#L122

Added line #L122 was not covered by tests
end

data = data_[:, freq, :, :]
return data
end
50 changes: 28 additions & 22 deletions src/SystemMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,51 +79,57 @@ function calculateSystemMatrixSNR(f::MPIFile, S::Array; numPeriodAverages=1, num
end

function calculateSystemMatrixSNRInner(S, SNR, J, R, K, N, gridSize::NTuple{D,Int}) where D
for j=1:J
for r=1:R
for j=1:J # Periods
for r=1:R # Channels
# precalc on entire FOV
for k=1:K
SBG = S[(N+1):end,k,r,j]
SFG = S[1:N,k,r,j]
for k=1:K # Frequencies
SBG = S[(N+1):end, k, r, j]
SFG = S[1:N, k, r, j]
meanBG = mean(SBG)
noise = mean(abs.(SBG .- meanBG))
noise = length(SBG) > 1 ? noise = mean(abs.(SBG .- meanBG)) : abs.(meanBG) # Prevent Inf due to substracting the same value
signal = median( abs.(SFG.-meanBG) )
SNR[k,r,j] = signal / noise
SNR[k, r, j] = !iszero(noise) ? signal / noise : 0
end

# generate mask representing signal region
idx = sortperm(vec(SNR[:,r,j]), rev=true)
idx = sortperm(vec(SNR[:, r, j]), rev=true)
mask = zeros(Bool, N)
for q = 1:20
SFG = abs.(S[1:N,idx[q],r,j])
SFG = abs.(S[1:N, idx[q], r, j])
maxSFG = maximum(SFG)
mask[ SFG .> maxSFG*0.90 ] .= true
end

#@info sum(mask)/length(mask)
# calc SNR on mask
for k=1:K
SBG = S[(N+1):end,k,r,j]
SFG = S[1:N,k,r,j]
for k=1:K # Frequencies
SBG = S[(N+1):end, k, r, j]
SFG = S[1:N, k, r, j]
meanBG = mean(SBG)
noise = mean(abs.(SBG .- meanBG))
κ = abs.(SFG.-meanBG)
noise = length(SBG) > 1 ? noise = mean(abs.(SBG .- meanBG)) : abs.(meanBG) # Prevent Inf due to substracting the same value
κ = abs.(SFG .- meanBG)
#κ_ = mapwindow(median!, reshape(κ, gridSize), ntuple(d->5,D))
#signal = maximum( κ_ )
signal = median( κ[mask .== true] )
#signal = maximum( κ[mask .== true] )

if signal > 3.5*noise
phaseMaskA = angle.(SFG.-meanBG) .> 0
phaseMaskB = angle.(SFG.-meanBG) .<= 0
phaseMask = sum(phaseMaskA) > sum(phaseMaskB) ? phaseMaskA : phaseMaskB
#signal = maximum( κ[mask .== true] )
# phaseMaskA = angle.(SFG.-meanBG) .> 0
# phaseMaskB = angle.(SFG.-meanBG) .<= 0
# phaseMask = sum(phaseMaskA) > sum(phaseMaskB) ? phaseMaskA : phaseMaskB
# signal = maximum( κ[mask .== true] )
κmax = maximum(κ)
signal = median( κ[(κ .> κmax*0.9 ) ] )
#signal = mean( κ[ mask .&& phaseMask ] )
signal = median( κ[(κ .> κmax*0.9 ) ] )
# signal = mean( κ[ mask .&& phaseMask ] )
end
SNR[k,r,j] = signal / noise

SNR[k, r, j] = signal / noise
end
end
end
SNR[:,:,:] .= median(SNR,dims=3)

SNR[:, :, :] .= median(SNR,dims=3)

return
end
#=
Expand Down
Loading