Skip to content

Commit

Permalink
Change getindex to return KeyError (#62)
Browse files Browse the repository at this point in the history
* Change `getindex` to return KeyErrror

* Fix errors with missing induction factor

* Some checks

* Fix stupid logic mistake

* Remove warning

* Fix tests
  • Loading branch information
jonschumacher authored Sep 27, 2023
1 parent 5a2f6e3 commit 107ffaf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
20 changes: 8 additions & 12 deletions src/Conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ export getFFdataPerPos, prepareAsMDFSingleMeasurement, convertCustomSF, blockAve


function setparam!(params::Dict, parameter, value)
if value != nothing
if !(isnothing(value) || ismissing(value))
params[parameter] = value
end
end

# we do not support all conversion possibilities
function loadDataset(f::MPIFile; frames=1:acqNumFrames(f), applyCalibPostprocessing=false,
numPeriodAverages=1, numPeriodGrouping=1, experimentNumber=nothing,
numPeriodAverages=1, numPeriodGrouping=1, experimentNumber=missing,
fixDistortions=false, kargs...)
params = loadMetadata(f)

if experimentNumber != nothing
if !ismissing(experimentNumber)
params[:experimentNumber] = experimentNumber
end

Expand Down Expand Up @@ -63,7 +63,7 @@ function loadDataset(f::MPIFile; frames=1:acqNumFrames(f), applyCalibPostprocess
cat(zeros(Bool,acqNumFGFrames(f)),ones(Bool,acqNumBGFrames(f)), dims=1))

snr = calibSNR(f)
if snr == nothing
if isnothing(snr)
@info "calculate SNR"
snr = calculateSystemMatrixSNR(f, data, numPeriodAverages=numPeriodAverages, numPeriodGrouping=numPeriodGrouping)
end
Expand Down Expand Up @@ -151,8 +151,6 @@ function loadMeasParams(f, params = Dict{Symbol,Any}(); skipMeasData = false)
return params
end



function appendBGDataset(params::Dict, filenameBG::String; kargs...)
params = MPIFile(filenameBG) do fBG
appendBGDataset(params, fBG; kargs...)
Expand All @@ -171,7 +169,6 @@ function appendBGDataset(params::Dict, fBG::MPIFile; frames=1:acqNumFrames(fBG))
return params
end


isConvertibleToMDF(f::MPIFile) = true
function isConvertibleToMDF(f::BrukerFile)
# check if raw data is consistent
Expand All @@ -196,16 +193,15 @@ function saveasMDF(filenameOut::String, filenameIn::String; kargs...)
end

function saveasMDF(filenameOut::String, f::MPIFile; filenameBG = nothing, enforceConversion=false, kargs...)

# This is a hack. Needs to be fixed properly
if(haskey(kargs, :SNRThresh) || haskey(kargs, :sparsityTrafoRedFactor)) && calibSNR(f) != nothing
if(haskey(kargs, :SNRThresh) || haskey(kargs, :sparsityTrafoRedFactor)) && !isnothing(calibSNR(f))
compressCalibMDF(filenameOut, f; kargs...)
return
end

if enforceConversion || isConvertibleToMDF(f)
params = loadDataset(f;kargs...)
if filenameBG != nothing
params = loadDataset(f; kargs...)
if !isnothing(filenameBG)
appendBGDataset(params, filenameBG)
end
saveasMDF(filenameOut, params)
Expand Down Expand Up @@ -746,7 +742,7 @@ function saveasMDF(file::HDF5.File, params::Dict{Symbol,Any})
write(file, "/scanner/topology", get(params,:scannerTopology,"FFP"))

# acquisition parameters
write(file, "/acquisition/numAverages", params[:acqNumAverages])
write(file, "/acquisition/numAverages", params[:acqNumAverages])
write(file, "/acquisition/numFrames", get(params,:acqNumFrames,1))
write(file, "/acquisition/numPeriodsPerFrame", get(params,:acqNumPeriodsPerFrame,1))
write(file, "/acquisition/startTime", "$( get(params,:acqStartTime, Dates.unix2datetime(time())) )")
Expand Down
12 changes: 3 additions & 9 deletions src/MDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ function h5haskey(filename, parameter)
end
end

function getindex(f::MDFFile, parameter)
if haskey(f.file, parameter)
return read(f.file, parameter)
else
return nothing
end
end
getindex(f::MDFFile, parameter) = read(f.file, parameter)

function getindex(f::MDFFile, parameter, default)
#if !haskey(f.param_cache,parameter)
Expand Down Expand Up @@ -106,8 +100,8 @@ studyUuid(f::MDFFileV2) = @keyrequired UUID(f["/study/uuid"])
studyDescription(f::MDFFileV1)::Union{String, Missing} = "n.a."
studyDescription(f::MDFFileV2)::Union{String, Missing} = @keyrequired f["/study/description"]
function studyTime(f::MDFFile)
t = f["/study/time"]
if typeof(t)==String
t = @keyoptional f["/study/time"]
if typeof(t) == String
return DateTime(t)
else
return nothing
Expand Down
7 changes: 5 additions & 2 deletions src/MDFInMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,11 @@ function inMemoryMDFFromMDFFileV2(mdfFile::MDFFileV2)::MDFv2InMemory
end

# Add measurements data
measDataRaw(mdf, mdfFile.mmap_measData)
if !isnothing(mdfFile.mmap_measData)
measDataRaw(mdf, mdfFile.mmap_measData)
else
@warn "The measurement data could not be read. Please check closely."
end

return mdf
end
Expand Down Expand Up @@ -1477,7 +1481,6 @@ measObservedDriveField(mdf::MDFv2InMemory, measDriveFields) = mdf.custom["measOb
measAppliedDriveField(mdf::MDFv2InMemory) = @keyoptional mdf.custom["measAppliedDriveField"]
measAppliedDriveField(mdf::MDFv2InMemory, measTransmit) = mdf.custom["measAppliedDriveField"] = measTransmit


auxiliaryData(mdf::MDFv2InMemory) = @keyoptional mdf.custom["auxiliaryData"]
auxiliaryData(mdf::MDFv2InMemory, auxiliaryData) = mdf.custom["auxiliaryData"] = auxiliaryData

Expand Down
9 changes: 9 additions & 0 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ function getMeasurements(f::MPIFile, neglectBGFrames=true;
if tfCorrection && !measIsTFCorrected(f)
tf = rxTransferFunction(f)
inductionFactor = rxInductionFactor(f)
if ismissing(inductionFactor)
@warn "The file is missing the induction factor. The induction factor will be set to 1."
inductionFactor = ones(Float64, rxNumChannels(f))
end

J = size(data,1)
dataF = rfft(data, 1)
Expand Down Expand Up @@ -323,6 +327,11 @@ function getMeasurementsFD(f::MPIFile, args...;
if tfCorrection && !measIsTFCorrected(f)
tf = rxTransferFunction(f)
inductionFactor = rxInductionFactor(f)
if ismissing(inductionFactor)
@warn "The file is missing the induction factor. The induction factor will be set to 1."
inductionFactor = ones(Float64, rxNumChannels(f))
end

data[2:end,:,:,:] ./= tf[2:end,:,:,:]

if all(tf[1,:,:,:] .!= 0) && !any(isnan.(tf[1,:,:,:]))
Expand Down

2 comments on commit 107ffaf

@jonschumacher
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92315

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.0 -m "<description of version>" 107ffaf80174584de50be67d501c64cef3e84a46
git push origin v0.14.0

Please sign in to comment.