From 4a6e0066473d0735612be69bf9de5a68db8d5b41 Mon Sep 17 00:00:00 2001 From: Tim Jarsky Date: Tue, 26 Nov 2024 14:43:40 -0800 Subject: [PATCH] PSX changes to baseline, peak, and tau calculations --- Packages/MIES/MIES_SweepFormula_PSX.ipf | 38 +++++++++++++------- Packages/MIES/MIES_WaveDataFolderGetters.ipf | 21 ++++++----- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Packages/MIES/MIES_SweepFormula_PSX.ipf b/Packages/MIES/MIES_SweepFormula_PSX.ipf index c66a6eae7a..997f5878c6 100644 --- a/Packages/MIES/MIES_SweepFormula_PSX.ipf +++ b/Packages/MIES/MIES_SweepFormula_PSX.ipf @@ -650,18 +650,26 @@ static Function [variable first, variable last] PSX_GetSingleEventRange(WAVE psx index = limit(index, 0, numEvents - 1) - offset = PSX_DEFAULT_RANGE_FACTOR * psxEvent[index][%tau] + // TODO calculate average tau without NaNs + // calculate gaussian distribution, take the tau at 2 * sigma for all events + // can be done once as it does not depend on accept/reject + // this is then used for the offset here instead of 7 * tau + + // don't store in psxEvent + + offset = 7 * psxEvent[index][%tau] if(IsNaN(offset)) offset = PSX_DEFAULT_X_START_OFFSET endif + first = psxEvent[index][%peak_t] - 0.5 + if(index == numEvents - 1) - first = psxEvent[index][%peak_t] - offset - last = psxEvent[index][%post_min_t] + offset + // TODO use min take the end of the range if it is smaller + last = psxEvent[index][%peak_t] + offset else - first = psxEvent[index][%peak_t] - offset - last = psxEvent[index + 1][%peak_t] - 0.5 + last = min(psxEvent[index][%peak_t] + offset, psxEvent[index + 1][%peak_t] - 0.5) endif return [first, last] @@ -674,14 +682,14 @@ static Function [variable start, variable stop] PSX_GetEventFitRange(WAVE sweepD variable calcLength, maxLength - start = psxEvent[eventIndex][%post_min_t] + start = psxEvent[eventIndex][%peak_t] maxLength = 10 * JWN_GetNumberFromWaveNote(psxEvent, SF_META_USER_GROUP + PSX_JWN_PARAMETERS + "/psxKernel/decayTau") if(eventIndex == (DimSize(psxEvent, ROWS) - 1)) calcLength = maxLength else - calcLength = min((psxEvent[eventIndex + 1][%post_min_t] - start) * 0.9, maxLength) + calcLength = min((psxEvent[eventIndex + 1][%peak_t] - start) * 0.9, maxLength) endif if(calcLength == 0) @@ -705,10 +713,10 @@ End /// \endrst static Function PSX_FitEventDecay(WAVE sweepDataOffFilt, WAVE psxEvent, variable maxTauFactor, WAVE/WAVE eventFit, variable eventIndex) - variable post_min_t, n_min_t, err, decayTau, fitRange, overrideTau + variable startTime, endTime, err, decayTau, fitRange, overrideTau string comboKey - [post_min_t, n_min_t] = PSX_GetEventFitRange(sweepDataOffFilt, psxEvent, eventIndex) + [startTime, endTime] = PSX_GetEventFitRange(sweepDataOffFilt, psxEvent, eventIndex) DFREF currDFR = GetDataFolderDFR() SetDataFolder NewFreeDataFolder() @@ -719,7 +727,7 @@ static Function PSX_FitEventDecay(WAVE sweepDataOffFilt, WAVE psxEvent, variable Make/FREE/D/N=3 coefWave AssertOnAndClearRTError() - CurveFit/Q/N=1/NTHR=1/M=0/W=2 exp_XOffset, kwCWave=coefWave, sweepDataOffFilt(post_min_t, n_min_t)/D/C=constraints; err = GetRTError(1) + CurveFit/Q/N=1/NTHR=1/M=0/W=2 exp_XOffset, kwCWave=coefWave, sweepDataOffFilt(startTime, endTime)/D/C=constraints; err = GetRTError(1) WAVE fit = MakeWaveFree($"fit__free_") @@ -750,7 +758,7 @@ static Function PSX_FitEventDecay(WAVE sweepDataOffFilt, WAVE psxEvent, variable return NaN endif - fitRange = n_min_t - post_min_t + fitRange = endTime - startTime if(IsFinite(decayTau) && decayTau > maxTauFactor * fitRange) psxEvent[eventIndex][%$"Fit manual QC call"] = PSX_REJECT @@ -1574,6 +1582,10 @@ threadsafe static Function PSX_CalculateRiseTimeImpl(WAVE psxEvent, WAVE sweepDa xStart = psxEvent[index][%peak_t] yStart = sweepDataOffFilt(xStart) + + // TODO calculate xEnd by searching for a minimum in [xstart, xstart + 3 times the rise time of the psxKernel] + // or maximum depending on kernelAmpSign + // write into psxEvent and call it "peak time in filtered sweep wave" this is the former post_min_t xEnd = psxEvent[index][%post_min_t] yEnd = psxEvent[index][%post_min] @@ -1829,6 +1841,8 @@ static Function PSX_UpdateOffsetInAllEventGraph(string win) ASSERT(WaveExists(singleEvent), "Non-existing single event wave") [first, last] = PSX_GetSingleEventRange(psxEvent, i) + + // TODO don't have constant baseline in the end which looks like made up data, NaN it Duplicate/FREE/R=(first, last) sweepDataOffFilt, singleEventRaw @@ -1838,7 +1852,7 @@ static Function PSX_UpdateOffsetInAllEventGraph(string win) yOffset = sweepDataOffFilt(psxEvent[i][%peak_t]) break case PSX_HORIZ_OFFSET_PEAK: - xOffset = first - psxEvent[i][%post_min_t] + xOffset = first - psxEvent[i][%post_min_t] // TODO new value: peak time in filtered sweep wave yOffset = 0 break default: diff --git a/Packages/MIES/MIES_WaveDataFolderGetters.ipf b/Packages/MIES/MIES_WaveDataFolderGetters.ipf index bc87b11667..56a698785f 100644 --- a/Packages/MIES/MIES_WaveDataFolderGetters.ipf +++ b/Packages/MIES/MIES_WaveDataFolderGetters.ipf @@ -8212,14 +8212,17 @@ End /// /// Cols: /// - 0/index: Event index -/// - 1/peak_t: Event time [ms] -/// - 2/peak: Event amplitude in deconvoluted data [y unit of data] -/// - 3/post_min: Minimum of filtered and offsetted data in the range [time, time + 2ms] -/// - 4/post_min_t: X location of [2] -/// - 5/pre_max: Maximum of filtered and offsetted data in the range [time - 2ms, time], averaged over +/- 0.1 ms -/// - 6/pre_max_t: X location of [5] -/// - 7/rel_peak: Relative amplitude: [2] - [4] -/// - 8/isi: Time difference to previous event [ms] +/// - 1/onset_t: Event time [ms] // TODO change to deconvolved peak time +/// - 2/onset: Event amplitude in deconvoluted data [y unit of data] +/// - 3/peak: ... +/// - 4/peak_t: X location of [3] +/// see PSX_CalculateriseTimeImpl +/// - 5/baseline: ... +/// - 6/baseline_t: X location of [5] +// TODO neg polarity (kernel amp sign) -> maximum in pre_max_t +// minimum for positive kernel amp sign, average over +/- 5 sample points, go from [time - 4ms, time] +/// - 7/amplitude: Relative amplitude: [3] - [5] stays the same +/// - 8/iei: Time difference to previous event (inter event interval) [ms] /// - 9/tau: Decay constant tau of exponential fit /// - 10/Fit manual QC call: One of @ref PSXStates /// - 11/Fit result: 1 for success, everything smaller than 0 is failure: @@ -8227,6 +8230,8 @@ End /// - `]inf, -10000]`: Custom error codes, one of @ref FitEventDecayCustomErrors /// - 12/Event manual QC call: One of @ref PSXStates /// - 13/Rise Time: rise time as calculated by PSX_CalculateRiseTime() +/// +/// TODO: no upgrade, clear data and let the user restart Function/WAVE GetPSXEventWaveAsFree() variable versionOfWave = PSX_WAVE_VERSION