Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
t-b committed Jan 24, 2025
1 parent 1e14749 commit 7203f65
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 62 deletions.
83 changes: 51 additions & 32 deletions Packages/MIES/MIES_AnalysisFunctions_PatchSeq.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,7 @@ End
/// @retval errMsg error message if both `fitOffset` and `fitSlope` are null
static Function [WAVE/D fitOffset, WAVE/D fitSlope, string errMsg] PSQ_DS_FitFrequencyCurrentData(string device, variable sweepNo, WAVE/Z apfreq, WAVE/Z DAScales, [variable singleFit, variable writeEnoughPointsLBN])

variable i, numPoints, numFits, first, last, initialFit, idx, elem, hasEnoughPoints
variable i, numPoints, numFits, first, last, initialFit, idx, elem, hasEnoughPoints, DAScaleRef
string line, databrowser, key, msg
string str = ""

Expand All @@ -2195,7 +2195,10 @@ static Function [WAVE/D fitOffset, WAVE/D fitSlope, string errMsg] PSQ_DS_FitFre
endif

// sort in ascending DAScale order while keeping track where we are
//DAScaleRef = DAScales[inf]
DAScaleRef = DAScales[Inf]

[WAVE DAScalesSorted, WAVE apfreqSorted] = SortKeyAndData(DAScales, apfreq)
WaveClear DAScales, apfreq

// original data:
// apfreq: {5, 8, 9, 9, 15}
Expand All @@ -2207,8 +2210,8 @@ static Function [WAVE/D fitOffset, WAVE/D fitSlope, string errMsg] PSQ_DS_FitFre
// DAScale: {50, 100, 130, 200}
// deletedIndizes: {3}
// numPoints = 4
[WAVE apfreqFiltered, WAVE DAScalesFiltered, WAVE deletedIndizes, numPoints] = PSQ_DS_FilterFrequencyCurrentData(apfreq, DAScales)
WaveClear apfreq, DAScales
[WAVE apfreqFiltered, WAVE DAScalesFiltered, WAVE deletedIndizes, numPoints] = PSQ_DS_FilterFrequencyCurrentData(apfreqSorted, DAScalesSorted)
WaveClear DAScalesSorted, apfreqSorted

hasEnoughPoints = (numPoints >= PSQ_DA_NUM_POINTS_LINE_FIT)

Expand All @@ -2223,18 +2226,23 @@ static Function [WAVE/D fitOffset, WAVE/D fitSlope, string errMsg] PSQ_DS_FitFre
return [$"", $"", "Not enough points for fit"]
endif

//PSQ_DS_SortDataForFit
if(singleFit)
numPoints = PSQ_DA_NUM_POINTS_LINE_FIT

// TODO for filled in data we need to find the right sequence of
// apFreqFiltered/DAScalesFiltered so that we get a useful fit
if(DAScalesFiltered[Inf] != WaveMax(DAScalesFiltered))
ASSERT(singleFit, "Resorting onlyfor singleFit")
endif
Duplicate/FREE/RMD=[0, PSQ_DA_NUM_POINTS_LINE_FIT - 1] apfreqFiltered, apfreqFinal
Duplicate/FREE/RMD=[0, PSQ_DA_NUM_POINTS_LINE_FIT - 1] DAScalesFiltered, DAScalesFinal

if(singleFit && hasEnoughPoints)
DeletePoints/M=(ROWS) 0, (numPoints - PSQ_DA_NUM_POINTS_LINE_FIT), apFreqFiltered, DAScalesFiltered
numPoints = PSQ_DA_NUM_POINTS_LINE_FIT
// now we need to grab PSQ_DA_NUM_POINTS_LINE_FIT points
// going from back to front starting at DAScaleRef
FindValue/R/V=(DAScaleRef) DAScalesFiltered
ASSERT(V_Value >= 0, "Invalid DAScaleRef")
apfreqFinal[] = apfreqFiltered[V_Value - PSQ_DA_NUM_POINTS_LINE_FIT + 1 + p]
DAScalesFinal[] = DAScalesFiltered[V_Value - PSQ_DA_NUM_POINTS_LINE_FIT + 1 + p]
else
WAVE apfreqFinal = apfreqFiltered
WAVE DAScalesFinal = DAScalesFiltered
endif
WAVEClear DAScalesFiltered, apfreqFiltered

numFits = (numPoints - PSQ_DA_NUM_POINTS_LINE_FIT) + 1

Expand All @@ -2243,12 +2251,12 @@ static Function [WAVE/D fitOffset, WAVE/D fitSlope, string errMsg] PSQ_DS_FitFre
first = i
last = (i + PSQ_DA_NUM_POINTS_LINE_FIT) - 1

WAVE apfreqSlice = DuplicateSubRange(apfreqFiltered, first, last)
WAVE apfreqSlice = DuplicateSubRange(apfreqFinal, first, last)
/// @todo prefer %f over %g due to https://github.com/AllenInstitute/MIES/issues/1863
sprintf line, "freq%d = [%s]\r", i, NumericWaveToList(apfreqSlice, ",", format = PERCENT_F_MAX_PREC, trailSep = 0)
str += line

WAVE DaScalesSlice = DuplicateSubRange(DAScalesFiltered, first, last)
WAVE DaScalesSlice = DuplicateSubRange(DAScalesFinal, first, last)
sprintf line, "dascale%d = [%s]\r", i, NumericWaveToList(DaScalesSlice, ",", format = PERCENT_F_MAX_PREC, trailSep = 0)
str += line
endfor
Expand Down Expand Up @@ -2314,7 +2322,7 @@ static Function [WAVE/D fitOffset, WAVE/D fitSlope, string errMsg] PSQ_DS_FitFre
return [$"", $"", "All fit results are NaN"]
endif

sprintf msg, "apfreqFiltered=(%s), DAScalesFiltered=(%s), fitOffset=(%s), fitSlope=(%s)", NumericWaveToList(apfreqFiltered, ", ", trailSep = 0), NumericWaveToList(DAScalesFiltered, ", ", trailSep = 0), NumericWaveToList(fitOffset, ", ", trailSep = 0), NumericWaveToList(fitSlope, ", ", trailSep = 0)
sprintf msg, "apfreqFinal=(%s), DAScalesFinal=(%s), fitOffset=(%s), fitSlope=(%s)", NumericWaveToList(apfreqFinal, ", ", trailSep = 0), NumericWaveToList(DAScalesFinal, ", ", trailSep = 0), NumericWaveToList(fitOffset, ", ", trailSep = 0), NumericWaveToList(fitSlope, ", ", trailSep = 0)
DEBUGPRINT(msg)

return [fitOffset, fitSlope, ""]
Expand Down Expand Up @@ -3160,7 +3168,7 @@ End

static Function PSQ_DS_AddDAScaleFillinBetweenPosAndNegSlope(string device, variable sweepNo, variable headstage, [variable fromRhSuAd])

variable emptySCI, numFitSlopes, i
variable emptySCI, numFitSlopes, i, numEntries
string type, msg
variable centerDASCale = NaN

Expand All @@ -3173,22 +3181,33 @@ static Function PSQ_DS_AddDAScaleFillinBetweenPosAndNegSlope(string device, vari
WAVE numericalValues = GetLBNumericalValues(device)
WAVE textualValues = GetLBTextualValues(device)

[WAVE fitSlope, emptySCI] = PSQ_DS_GetLabnotebookData(numericalValues, textualValues, sweepNo, headstage, PSQ_DS_FI_SLOPE, fromRhSuAd = fromRhSuAd)
[WAVE negSlopePassed, emptySCI] = PSQ_DS_GetLabnotebookData(numericalValues, textualValues, sweepNo, headstage, PSQ_DS_FI_NEG_SLOPE_PASS, fromRhSuAd = fromRhSuAd)
[WAVE DAScale, emptySCI] = PSQ_DS_GetLabnotebookData(numericalValues, textualValues, sweepNo, headstage, PSQ_DS_DASCALE, fromRhSuAd = fromRhSuAd)

ASSERT(DimSize(DAScale, ROWS) == (DimSize(fitSlope, ROWS) + 1), "Non-matching fitSlope and DAScale waves")
// [WAVE fitSlope, emptySCI] = PSQ_DS_GetLabnotebookData(numericalValues, textualValues, sweepNo, headstage, PSQ_DS_FI_SLOPE, fromRhSuAd = fromRhSuAd)

numFitSlopes = DimSize(fitSlope, ROWS)
for(i = 1; i < numFitSlopes; i += 1)
if(IsFinite(fitSlope[i - 1]) \
&& IsFinite(fitSlope[i]) \
&& (sign(fitSlope[i - 1]) != sign(fitSlope[i])))
centerDAScale = round((DAScale[i] + DAScale[i + 1]) / 2)
endif
endfor
numEntries = DimSize(DAScale, ROWS)
ASSERT(numEntries == (DimSize(negSlopePassed, ROWS) + 1) , "Non-matching negSlopePassedAll and DAScale waves")

FindValue/R/V=(0) negSlopePassed
if(V_Value >= 0)
centerDAScale = round((DAScale[V_Value] + DAScale[V_Value + 1]) / 2)
endif

// for(i = numEntries - 1; i >= 0; i += 1)
// endfor

// numFitSlopes = DimSize(fitSlope, ROWS)
// for(i = numFitSlopes - 1; i >= 1; i -= 1)
// if(IsFinite(fitSlope[i - 1]) \
// && IsFinite(fitSlope[i]) \
// && (sign(fitSlope[i + 1]) != sign(fitSlope[i])))
// centerDAScale = round((DAScale[i] + DAScale[i + 1]) / 2)
// endif
// endfor

#ifdef DEBUGGING_ENABLED
sprintf msg, "centerDAScale %g: DAScales=%s; fitSlopes=%s", centerDAScale, NumericWaveToList(DAScale, ", ", trailSep = 0), NumericWaveToList(fitSlope, ", ", trailSep = 0)
sprintf msg, "centerDAScale %g: DAScales=%s; fitSlopes=%s", centerDAScale, NumericWaveToList(DAScale, ", ", trailSep = 0), NumericWaveToList(negSlopePassed, ", ", trailSep = 0)
DEBUGPRINT(msg)
#endif

Expand Down Expand Up @@ -3222,7 +3241,7 @@ End
static Function PSQ_DS_AdaptiveIsFinished(string device, variable sweepNo, variable headstage, variable numSweepsWithSaturation, [variable fromRhSuAd])

string key
variable measuredAllFutureDAScales, numFound, emptySCI, slopeFillinRequired
variable measuredAllFutureDAScales, numFound, emptySCI, slopeFillinDone

if(ParamIsDefault(fromRhSuAd))
fromRhSuAd = 0
Expand Down Expand Up @@ -3260,10 +3279,10 @@ static Function PSQ_DS_AdaptiveIsFinished(string device, variable sweepNo, varia
endif

if(PSQ_DS_NegativefISlopePassingCriteria(numericalValues, textualValues, sweepNo, headstage, fromRhSuAd = fromRhSuAd))
key = CreateAnaFuncLBNKey(PSQ_DA_SCALE, PSQ_FMT_LBN_DA_AT_NEG_SLOPE_FILLIN, query = 1)
slopeFillinRequired = GetLastSettingIndepSCI(numericalValues, sweepNo, key, headstage, UNKNOWN_MODE, defValue = 0)
key = CreateAnaFuncLBNKey(PSQ_DA_SCALE, PSQ_FMT_LBN_DA_AT_NEG_SLOPE_FILLIN, query = 1)
slopeFillinDone = GetLastSettingIndepSCI(numericalValues, sweepNo, key, headstage, UNKNOWN_MODE, defValue = 0)

if(slopeFillinRequired)
if(slopeFillinDone)
return 1
endif

Expand Down
2 changes: 2 additions & 0 deletions Packages/tests/Basic/UTF_Utils_Algorithm.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -1291,5 +1291,7 @@ static Function TestSortKeyAndData()
[WAVE keySorted, WAVE dataSorted] = SortKeyAndData(key, data)

CHECK_EQUAL_WAVES(keySorted, {1, 2, 3})
CHECK_EQUAL_VAR(DimSize(KeySorted, COLS), 0)
CHECK_EQUAL_WAVES(dataSorted, {-2, -3, -1})
CHECK_EQUAL_VAR(DimSize(dataSorted, COLS), 0)
End
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ static Function PS_DS_AD12_preAcq(string device)
AFH_AddAnalysisParameter("PSQ_DaScale_Adapt_DA_0", "BaselineRMSLongThreshold", var = 0.5)
AFH_AddAnalysisParameter("PSQ_DaScale_Adapt_DA_0", "BaselineRMSShortThreshold", var = 0.07)

ST_SetStimsetParameter("PSQ_DaScale_Adapt_DA_0", "Total number of steps", var = 6)
ST_SetStimsetParameter("PSQ_DaScale_Adapt_DA_0", "Total number of steps", var = 7)

AFH_AddAnalysisParameter("PSQ_DaScale_Adapt_DA_0", "AbsFrequencyMinDistance", var = 1.1)

Expand Down Expand Up @@ -2119,11 +2119,14 @@ static Function PS_DS_AD12([string str])

WAVE wv = PSQ_CreateOverrideResults(str, PSQ_TEST_HEADSTAGE, PSQ_DA_SCALE, opMode = PSQ_DS_ADAPT)

// TODO explanation
wv[][0][%APFrequency] = 25
wv[][1][%APFrequency] = 21 // future DAScale (8.4)
wv[][2][%APFrequency] = 21 // redoing future DAScale (8.4), passed f-I slope QC (1.) and got another future DAScale 5.95
wv[][3][%APFrequency] = 21.1 // passed f-I slope QC (2.)
// fillin for fI neg slope QC is not done as this would just be a duplicate DAScale entry
wv[][3][%APFrequency] = 21.1 //
wv[][4][%APFrequency] = 15 //
wv[][5][%APFrequency] = 10 //
wv[][6][%APFrequency] = 16 //

wv[][][%AsyncQC] = 1
wv[][][%BaselineQC] = 1
Expand All @@ -2134,35 +2137,35 @@ static Function PS_DS_AD12_REENTRY([string str])

variable sweepNo

sweepNo = 3
sweepNo = 6

WAVE/WAVE entries = GetEntries_IGNORE(str, sweepNo)

CHECK_EQUAL_TEXTWAVES(entries[%opMode], {PSQ_DS_ADAPT}, mode = WAVE_DATA)

CHECK_EQUAL_WAVES(entries[%setPass], {1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%sweepPass], {1, 0, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%sweepPassExceptBL], {1, 0, 1, 1}, mode = WAVE_DATA)

CHECK_EQUAL_WAVES(entries[%rmsShortPass], {1, 0, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%rmsLongPass], {1, NaN, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%baselinePass], {1, 0, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%asyncPass], {1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%samplingPass], {1, 1, 1, 1}, mode = WAVE_DATA)

CHECK_EQUAL_WAVES(entries[%futureDAScalesPass], {0, 1, 0, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%fiSlopeReachedPass], {0, 0, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%fiNegSlopePass], {0, 0, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%enoughFIPointsPass], {1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%validSlopePass], {1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%initialValidSlopePass], {1, NaN, NaN, NaN}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%sweepPass], {1, 0, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%sweepPassExceptBL], {1, 0, 1, 1, 1, 1, 1}, mode = WAVE_DATA)

CHECK_EQUAL_WAVES(entries[%rmsShortPass], {1, 0, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%rmsLongPass], {1, NaN, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%baselinePass], {1, 0, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%asyncPass], {1, 1, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%samplingPass], {1, 1, 1, 1, 1, 1, 1}, mode = WAVE_DATA)

CHECK_EQUAL_WAVES(entries[%futureDAScalesPass], {0, 1, 0, 0, 0, 0, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%fiSlopeReachedPass], {0, 0, 1, 0, 0, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%fiNegSlopePass], {0, 0, 1, 0, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%enoughFIPointsPass], {1, 1, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%validSlopePass], {1, 1, 1, 1, 1, 1, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%initialValidSlopePass], {1, NaN, NaN, NaN, NaN, NaN, NaN}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%fiSlopeReachedPassFromRhSuAd], {0, 0, 1}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%fiNegSlopesPassFromRhSuAd], {0, 0, 0}, mode = WAVE_DATA)

Make/FREE/D minDAScaleNormRef = {0.1536458333333333, NaN, NaN, NaN}
Make/FREE/D minDAScaleNormRef = {0.1536458333333333, NaN, NaN, NaN, NaN, NaN, NaN}
CHECK_EQUAL_WAVES(entries[%minDaScaleNorm], minDAScaleNormRef, mode = WAVE_DATA, tol = 1e-24)

Make/FREE/D maxDAScaleNormRef = {0.4609374999999999, NaN, NaN, NaN}
Make/FREE/D maxDAScaleNormRef = {0.4609374999999999, NaN, NaN, NaN, NaN, NaN, NaN}
CHECK_EQUAL_WAVES(entries[%maxDaScaleNorm], maxDAScaleNormRef, mode = WAVE_DATA, tol = 1e-24)

[WAVE apFreqRef, WAVE apFreqFromRhSuAd, WAVE DAScalesFromRhSuAd, WAVE sweepPassedFRomRhSuAd] = ExtractRefValuesFromOverride(sweepNo, baselineQC = entries[%baselinePass])
Expand All @@ -2172,16 +2175,20 @@ static Function PS_DS_AD12_REENTRY([string str])
CHECK_EQUAL_WAVES(entries[%dascaleFromRhSuAd], DAScalesFromRhSuAd, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%sweepPassFromRhSuAd], sweepPassedFRomRhSuAd, mode = WAVE_DATA)

Make/FREE/D maxSlopeRef = {3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10}
Make/FREE/D fiSlopeRef = {3.142857142857143e-10, 3.142857142857143e-10, -2e-10, -3.333333333333381e-12}
Make/FREE/D fiOffsetRef = {3, 3, 39, 21.3}
Make/FREE/T futureDAScalesRef = {"RegRhSuAd:7;Reg:9;", \
"RegRhSuAd:7;Reg:9;", \
"RegRhSuAd:7;Reg:9;Fillin:6;", \
"RegRhSuAd:7;Reg:9;Fillin:6;"}
Make/FREE/D maxSlopeRef = {3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10, 3.142857142857143e-10}
Make/FREE/D fiSlopeRef = {3.142857142857143e-10, 3.142857142857143e-10, -2e-10, 2.84e-10, -1.2e-10, -1.666666666666667e-10, -9e-10}
Make/FREE/D fiOffsetRef = {3, 3, 39, 4.060000000000002, 31.8, 38.33333333333334, 88}
Make/FREE/T futureDAScalesRef = {"RegRhSuAd:7;Reg:9;", \
"RegRhSuAd:7;Reg:9;", \
"RegRhSuAd:7;Reg:9;Fillin:6;", \
"RegRhSuAd:7;Reg:9;Fillin:6;RegPosNegSlope:14;", \
"RegRhSuAd:7;Reg:9;Fillin:6;RegPosNegSlope:14;RegPosNegSlope:17;", \
"RegRhSuAd:7;Reg:9;Fillin:6;RegPosNegSlope:14;RegPosNegSlope:17;FillinPosNegSlope:8;", \
"RegRhSuAd:7;Reg:9;Fillin:6;RegPosNegSlope:14;RegPosNegSlope:17;FillinPosNegSlope:8;"}

Make/FREE/D fiSlopesFromRhSuAdRef = {2e-10, 2e-10, 1e-10}
Make/FREE/D fiOffsetsFromRhSuAdRef = {8, 8, 10.5}
Make/FREE/D DAScalesRef = {7, 9, 9, 6}
Make/FREE/D DAScalesRef = {7, 9, 9, 6, 14, 17, 8}

CHECK_EQUAL_WAVES(entries[%maxSlope], maxSlopeRef, mode = WAVE_DATA, tol = 1e-24)
CHECK_EQUAL_WAVES(entries[%fiSlope], fiSlopeRef, mode = WAVE_DATA, tol = 1e-24)
Expand All @@ -2191,7 +2198,7 @@ static Function PS_DS_AD12_REENTRY([string str])
CHECK_EQUAL_WAVES(entries[%fiOffsetsFromRhSuAd], fiOffsetsFromRhSuAdRef, mode = WAVE_DATA, tol = 1e-24)
CHECK_EQUAL_WAVES(entries[%dascale], DAScalesRef, mode = WAVE_DATA, tol = 1e-24)

CHECK_EQUAL_WAVES(entries[%oorDAScale], {0, 0, 0, NaN}, mode = WAVE_DATA)
CHECK_EQUAL_WAVES(entries[%oorDAScale], {0, 0, 0, 0, 0, 0, NaN}, mode = WAVE_DATA)

printsomeValues(entries)

Expand Down

0 comments on commit 7203f65

Please sign in to comment.