Skip to content

Commit

Permalink
PSX changes to baseline, peak, and tau calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
timjarsky committed Nov 26, 2024
1 parent c239c3d commit 4a6e006
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
38 changes: 26 additions & 12 deletions Packages/MIES/MIES_SweepFormula_PSX.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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_")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
21 changes: 13 additions & 8 deletions Packages/MIES/MIES_WaveDataFolderGetters.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -8212,21 +8212,26 @@ 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:
/// - `]-10000, 0[`: CurveFit error codes
/// - `]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
Expand Down

0 comments on commit 4a6e006

Please sign in to comment.