Skip to content

Commit

Permalink
Merge pull request #2238 from AllenInstitute/bugfix/2238-better-error…
Browse files Browse the repository at this point in the history
…-message-basic-sf-operations

Enhance error message for mismatched waves with basic SF operations
  • Loading branch information
t-b authored Sep 3, 2024
2 parents 93f1b0e + ba7eb21 commit 177bfaa
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Packages/MIES/MIES_PulseAveraging.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -3448,12 +3448,12 @@ static Function/S PA_ShowImage(string win, STRUCT PulseAverageSettings &pa, STRU
scaleChanged = (DimOffset(img, ROWS) != refScaleLeft) || (DimOffset(img, ROWS) + (DimSize(img, ROWS) - 1) * DimDelta(img, ROWS) != refScaleRight)
xUnits = WaveUnits(set2[0][0], ROWS)

Make/FREE/N=(MAX_DIMENSION_COUNT) oldSizes = DimSize(img, p)
WAVE oldSizes = GetWaveDimensions(img)
EnsureLargeEnoughWave(img, indexShouldExist = requiredEntries, dimension = COLS, initialValue = NaN)
Redimension/N=(refScalePoints, -1) img
// inclusive scale must be set after redimension
SetScale/P x, refScaleLeft, refScaleDelta, xUnits, img
Make/FREE/N=(MAX_DIMENSION_COUNT) newSizes = DimSize(img, p)
WAVE newSizes = GetWaveDimensions(img)

if(!(mode != POST_PLOT_ADDED_SWEEPS \
|| !EqualWaves(oldSizes, newSizes, EQWAVES_DATA) \
Expand Down
29 changes: 25 additions & 4 deletions Packages/MIES/MIES_SweepFormula.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -3268,6 +3268,27 @@ static Function/WAVE SF_OperationEpochsImpl(string graph, WAVE/T epochPatterns,
return output
End

static Function SF_AssertOnMismatchedWaves(WAVE data0, WAVE data1, string opShort)

string msg, size0Str, size1Str
variable ret

ret = EqualWaves(data0, data1, EQWAVES_DIMSIZE)

if(ret)
return NaN
endif

WAVE size0 = GetWaveDimensions(data0)
WAVE size1 = GetWaveDimensions(data1)

size0Str = NumericWaveToList(size0, ", ", trailSep = 0)
size1Str = NumericWaveToList(size1, ", ", trailSep = 0)
sprintf msg, "%s: wave size mismatch [%s] vs [%s]", opShort, size0Str, size1Str

SFH_ASSERT(ret, msg)
End

static Function/WAVE SF_OperationMinus(variable jsonId, string jsonPath, string graph)

WAVE output = SF_IndexOverDataSetsForPrimitiveOperation(jsonId, jsonpath, graph, SF_OPSHORT_MINUS)
Expand Down Expand Up @@ -3296,7 +3317,7 @@ static Function/WAVE SF_OperationMinusImplDataSets(WAVE/Z data0, WAVE/Z data1)
CopyScales data1, result
return result
endif
SFH_ASSERT(EqualWaves(data0, data1, EQWAVES_DIMSIZE), "minus: wave size mismatch")
SF_AssertOnMismatchedWaves(data0, data1, SF_OPSHORT_MINUS)

MatrixOp/FREE result = data0 - data1
CopyScales data0, result
Expand Down Expand Up @@ -3331,7 +3352,7 @@ static Function/WAVE SF_OperationPlusImplDataSets(WAVE/Z data0, WAVE/Z data1)
CopyScales data1, result
return result
endif
SFH_ASSERT(EqualWaves(data0, data1, EQWAVES_DIMSIZE), "plus: wave size mismatch")
SF_AssertOnMismatchedWaves(data0, data1, SF_OPSHORT_PLUS)

MatrixOp/FREE result = data0 + data1
CopyScales data0, result
Expand Down Expand Up @@ -3466,7 +3487,7 @@ static Function/WAVE SF_OperationDivImplDataSets(WAVE/Z data0, WAVE/Z data1)
CopyScales data1, result
return result
endif
SFH_ASSERT(EqualWaves(data0, data1, EQWAVES_DIMSIZE), "div: wave size mismatch")
SF_AssertOnMismatchedWaves(data0, data1, SF_OPSHORT_DIV)

MatrixOp/FREE result = data0 / data1
CopyScales data0, result
Expand Down Expand Up @@ -3501,7 +3522,7 @@ static Function/WAVE SF_OperationMultImplDataSets(WAVE/Z data0, WAVE/Z data1)
CopyScales data1, result
return result
endif
SFH_ASSERT(EqualWaves(data0, data1, EQWAVES_DIMSIZE), "mult: wave size mismatch")
SF_AssertOnMismatchedWaves(data0, data1, SF_OPSHORT_MULT)

MatrixOp/FREE result = data0 * data1
CopyScales data0, result
Expand Down
17 changes: 12 additions & 5 deletions Packages/MIES/MIES_Utilities_WaveHandling.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ threadsafe Function EnsureLargeEnoughWave(WAVE wv, [variable indexShouldExist, v
Make/FREE/L/N=(MAX_DIMENSION_COUNT) targetSizes = -1
targetSizes[dimension] = indexShouldExist

Make/FREE/L/N=(MAX_DIMENSION_COUNT) oldSizes = DimSize(wv, p)
WAVE oldSizes = GetWaveDimensions(wv)

Redimension/N=(targetSizes[ROWS], targetSizes[COLS], targetSizes[LAYERS], targetSizes[CHUNKS]) wv

Expand Down Expand Up @@ -115,14 +115,22 @@ Function EnsureSmallEnoughWave(wv, [maximumSize])
maximumSize = MAXIMUM_WAVE_SIZE
endif

Make/FREE/I/N=(MAX_DIMENSION_COUNT) oldSizes
oldSizes[] = DimSize(wv, p)
WAVE oldSizes = GetWaveDimensions(wv)

if(oldSizes[ROWS] > maximumSize)
Redimension/N=(maximumSize, -1, -1, -1) wv
endif
End

/// @brief Return a wave with `MAX_DIMENSION_COUNT` entries with the size of each dimension
threadsafe Function/WAVE GetWaveDimensions(WAVE wv)

ASSERT_TS(WaveExists(wv), "Missing wave")
Make/FREE/D/N=(MAX_DIMENSION_COUNT) sizes = DimSize(wv, p)

return sizes
End

/// @brief Returns the size of the wave in bytes
threadsafe static Function GetWaveSizeImplementation(wv)
WAVE wv
Expand Down Expand Up @@ -616,8 +624,7 @@ threadsafe Function ReduceWaveDimensionality(WAVE/Z wv, [variable minDimension])
minDimension = ParamIsDefault(minDimension) ? COLS : minDimension
ASSERT_TS(IsInteger(minDimension) && minDimension >= ROWS && minDimension < MAX_DIMENSION_COUNT, "Invalid minDimension")
minDimension = limit(minDimension, COLS, MAX_DIMENSION_COUNT - 1)
Make/FREE/N=(MAX_DIMENSION_COUNT) waveSize
waveSize[] = DimSize(wv, p)
WAVE waveSize = GetWaveDimensions(wv)
for(i = MAX_DIMENSION_COUNT - 1; i >= minDimension; i -= 1)
if(waveSize[i] == 1)
waveSize[i] = 0
Expand Down
36 changes: 36 additions & 0 deletions Packages/tests/Basic/UTF_SweepFormula_Operations.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -2411,3 +2411,39 @@ static Function TPWithModelCell()
Make/D/FREE ref = {17.3667394014963}
CHECK_EQUAL_WAVES(ref, results, mode = WAVE_DATA)
End

// IUTF_TD_GENERATOR DataGenerators#GetBasicMathOperations
static Function BasicMathMismatchedWaves([string str])

string code, win, opShort, error

win = GetDataBrowserWithData()

sprintf code, "[1, 2] %s [[1, 2]]", str
try
WAVE/WAVE output = SF_ExecuteFormula(code, win, useVariables = 0)
FAIL()
catch
CHECK_NO_RTE()
endtry

strswitch(str)
case "*":
opShort = "mult"
break
case "/":
opShort = "div"
break
case "+":
opShort = "plus"
break
case "-":
opShort = "minus"
break
default:
FAIL()
endswitch

error = ROStr(GetSweepFormulaParseErrorMessage())
CHECK_EQUAL_STR(error, opShort + ": wave size mismatch [2, 0, 0, 0] vs [1, 2, 0, 0]")
End
32 changes: 31 additions & 1 deletion Packages/tests/Basic/UTF_Utils_WaveHandling.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ Function RUR_Works()

WAVE dup = RemoveUnusedRows(wv)

Make/FREE/N=(MAX_DIMENSION_COUNT) dims = DimSize(dup, p)
WAVE dims = GetWaveDimensions(dup)
CHECK_EQUAL_WAVES(dims, {4, 3, 2, 0}, mode = WAVE_DATA)
CHECK(!WaveRefsEqual(wv, dup))
End
Expand Down Expand Up @@ -1944,3 +1944,33 @@ Function ZWI_Works3()
End

/// @}

/// GetWaveDimensions
/// @{

Function GWD_ChecksParam()

try
GetWaveDimensions($"")
FAIL()
catch
CHECK_NO_RTE()
endtry
End

Function GWD_Works()

Make/FREE/N=0 wv
WAVE sizes = GetWaveDimensions(wv)
CHECK_EQUAL_WAVES(sizes, {0, 0, 0, 0}, mode = WAVE_DATA)

Make/FREE/N=(1, 2, 3, 4) wv
WAVE sizes = GetWaveDimensions(wv)
CHECK_EQUAL_WAVES(sizes, {1, 2, 3, 4}, mode = WAVE_DATA)

Make/FREE/N=(1, 2, 0, 4) wv
WAVE sizes = GetWaveDimensions(wv)
CHECK_EQUAL_WAVES(sizes, {1, 2, 0, 4}, mode = WAVE_DATA)
End

/// @}
8 changes: 8 additions & 0 deletions Packages/tests/UTF_DataGenerators.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,11 @@ static Function/WAVE GetDifferentGraphs()

return wv
End

static Function/WAVE GetBasicMathOperations()
Make/FREE/T op = {"+", "-", "*", "/"}

SetDimensionLabels(OP, "plus;minus;mult;div;", ROWS)

return op
End

0 comments on commit 177bfaa

Please sign in to comment.