Skip to content

Commit

Permalink
SFH: Adapt SFH_GetSweepsForFormula for new Epoch range standard
Browse files Browse the repository at this point in the history
- Epochs now start with an epoch point inside the epoch and end with a
  point outside the epoch. This was not taken into account by SF.

- Changed calculation to be mostly index based (integer)
  • Loading branch information
MichaelHuth committed Mar 19, 2024
1 parent 6ad9e25 commit 1f13155
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
25 changes: 17 additions & 8 deletions Packages/MIES/MIES_SweepFormula_Helpers.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ Function/WAVE SFH_GetSweepsForFormula(string graph, WAVE/WAVE range, WAVE/Z sele

variable i, j, rangeStart, rangeEnd, sweepNo, isSingleRange
variable chanNr, chanType, cIndex, isSweepBrowser
variable numSelected, index, numRanges, lastx
variable numSelected, index, numRanges, sweepSize, samplingInterval, samplingOffset
variable rangeStartIndex, rangeEndIndex
string dimLabel, device, dataFolder
ASSERT(WindowExists(graph), "graph window does not exist")

Expand Down Expand Up @@ -499,32 +500,40 @@ Function/WAVE SFH_GetSweepsForFormula(string graph, WAVE/WAVE range, WAVE/Z sele
continue
endif

sweepSize = DimSize(Sweep, ROWS)
samplingInterval = DimDelta(sweep, ROWS)
samplingOffset = DimOffset(sweep, ROWS)

numRanges = DimSize(adaptedRange, COLS)
for(j = 0; j < numRanges; j += 1)
rangeStart = adaptedRange[0][j]
rangeEnd = adaptedRange[1][j]
lastx = rightx(sweep) - DimDelta(sweep, ROWS)

rangeStartIndex = round((rangeStart - samplingOffset) / samplingInterval)
rangeEndIndex = round((rangeEnd - samplingOffset) / samplingInterval)

// Release 8c6e5da (EP_WriteEpochInfoIntoSweepSettings: Handle unacquired data, 2021-07-13) and before:
// we did not cap epoch ranges properly on aborted/shortened sweeps
// we also did not calculate the sampling points for TP and Stimesets exactly the same way
// Thus, if necessary we clip the data here.
if(WaveExists(epochRangeNames))
// complete epoch starting at or beyond sweep end
if(rangeStart >= lastx)
if(rangeStartIndex >= sweepSize)
continue
endif
rangeEnd = limit(rangeEnd, -inf, lastx)
rangeEndIndex = limit(rangeEndIndex, -inf, sweepSize)
endif

SFH_ASSERT(rangeStart < rangeEnd, "Starting range must be smaller than the ending range for sweep " + num2istr(sweepNo) + ".")
SFH_ASSERT(rangeStart == -inf || (IsFinite(rangeStart) && rangeStart >= leftx(sweep) && rangeStart < lastx), "Specified starting range not inside sweep " + num2istr(sweepNo) + ".")
SFH_ASSERT(rangeEnd == inf || (IsFinite(rangeEnd) && rangeEnd > leftx(sweep) && rangeEnd <= lastx), "Specified ending range not inside sweep " + num2istr(sweepNo) + ".")
Duplicate/FREE/R=(rangeStart, rangeEnd) sweep, rangedSweepData
SFH_ASSERT(rangeStartIndex < rangeEndIndex - 1, "Starting range must be smaller than the ending range for sweep " + num2istr(sweepNo) + ".")
SFH_ASSERT(rangeStartIndex == -inf || (IsFinite(rangeStartIndex) && rangeStartIndex >= 0 && rangeStartIndex < sweepSize), "Specified starting range not inside sweep " + num2istr(sweepNo) + ".")
SFH_ASSERT(rangeEndIndex == inf || (IsFinite(rangeEndIndex) && rangeEndIndex > 0 && rangeEndIndex <= sweepSize), "Specified ending range not inside sweep " + num2istr(sweepNo) + ".")
Duplicate/FREE/RMD=[rangeStartIndex, rangeEndIndex - 1] sweep, rangedSweepData

if(WaveExists(epochRangeNames))
Make/FREE/T entry = {epochRangeNames[j]}
JWN_SetWaveInWaveNote(rangedSweepData, SF_META_RANGE, entry)
else
// we write here on purpose the requested range
JWN_SetWaveInWaveNote(rangedSweepData, SF_META_RANGE, {rangeStart, rangeEnd})
endif

Expand Down
4 changes: 4 additions & 0 deletions Packages/doc/SweepFormula.rst
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ is determined from the epoch information of each sweep/channel/epoch data iterat
that sweep data is not included in the sweep data returned. If the same epoch is resolved multiple times from wildcard expressions or
multiple epoch names then it is included only once per sweep.
A given range as numbers or epoch extracts a subrange of data points from the sweep. The start and end time is converted to
closest integer indices, where the included points range from `startIndex` to `endIndex - 1`. This matches the general handling
of epochs in MIES, where the data point at the end time of an epoch is not part of the epoch range.
selectData is retrieved through the `select` operation. It selects for which sweeps and channels sweep data is returned.
`select` also allows to choose currently displayed sweeps or all existing sweeps as data source.
When the optional selectData argument is omitted, `select()` is used as default that includes all displayed sweeps and channels.
Expand Down
4 changes: 2 additions & 2 deletions Packages/tests/Basic/UTF_SweepFormula.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ static Function CheckSweepsFromData(WAVE/WAVE dataWref, WAVE sweepRef, variable
Duplicate/FREE/RMD=[][chanIndex[i]] sweepRef, sweepDataRef
Redimension/N=(-1) sweepDataRef
if(!ParamIsDefault(ranges))
Duplicate/FREE/RMD=[ranges[i][0], ranges[i][1]] sweepDataRef, sweepDataRanged
Duplicate/FREE/RMD=[ranges[i][0], ranges[i][1] - 1] sweepDataRef, sweepDataRanged
WAVE sweepDataRef = sweepDataRanged
endif

Expand Down Expand Up @@ -2704,7 +2704,7 @@ static Function TestOperationData()
str = "data(epochs([TestEpoch1], " + str + "), " + str + ")"
WAVE/WAVE dataWref = SF_ExecuteFormula(str, win, useVariables=0)
REQUIRE_EQUAL_VAR(DimSize(dataWref, ROWS), 1)
REQUIRE_EQUAL_VAR(DimSize(dataWref[0], ROWS), 6)
REQUIRE_EQUAL_VAR(DimSize(dataWref[0], ROWS), 5)

// range begin
str = "data([12, 10],select(channels(AD),[" + num2istr(sweepNo) + "],all))"
Expand Down

0 comments on commit 1f13155

Please sign in to comment.