From d00be91c0946e64315d11f327a77adba0a572a84 Mon Sep 17 00:00:00 2001 From: Thomas Braun Date: Mon, 2 Dec 2024 21:53:10 +0200 Subject: [PATCH] NumberFromList: Add it This is more succinct. --- ...isFunctions_MultiPatchSeq_SpikeControl.ipf | 4 ++-- Packages/MIES/MIES_Browser_Plotter.ipf | 10 ++++---- Packages/MIES/MIES_Configuration.ipf | 12 +++++----- Packages/MIES/MIES_DAC-Hardware.ipf | 2 +- Packages/MIES/MIES_Epochs.ipf | 8 +++---- Packages/MIES/MIES_GuiUtilities.ipf | 2 +- .../MIES/MIES_MiesUtilities_Recreation.ipf | 2 +- Packages/MIES/MIES_MiesUtilities_Sweep.ipf | 2 +- Packages/MIES/MIES_PressureControl.ipf | 24 +++++++++---------- Packages/MIES/MIES_SweepFormula_PSX.ipf | 2 +- Packages/MIES/MIES_Utilities_Conversions.ipf | 2 +- Packages/MIES/MIES_Utilities_GUI.ipf | 2 +- Packages/MIES/MIES_Utilities_List.ipf | 10 ++++++++ Packages/MIES/MIES_WaveBuilder.ipf | 2 +- Packages/tests/Basic/UTF_Utils_List.ipf | 24 +++++++++++++++++++ 15 files changed, 71 insertions(+), 37 deletions(-) diff --git a/Packages/MIES/MIES_AnalysisFunctions_MultiPatchSeq_SpikeControl.ipf b/Packages/MIES/MIES_AnalysisFunctions_MultiPatchSeq_SpikeControl.ipf index 2d47fbb08e..e69b84c7df 100644 --- a/Packages/MIES/MIES_AnalysisFunctions_MultiPatchSeq_SpikeControl.ipf +++ b/Packages/MIES/MIES_AnalysisFunctions_MultiPatchSeq_SpikeControl.ipf @@ -549,8 +549,8 @@ static Function/WAVE SC_RegionBlanked(WAVE data, variable totalOnsetDelay, WAVE/ numEntries = DimSize(oodDAQRegion, ROWS) Make/D/FREE/N=(numEntries) regionStart, regionEnd - regionStart[] = str2num(StringFromList(0, oodDAQRegion[p], "-")) + totalOnsetDelay - regionEnd[] = str2num(StringFromList(1, oodDAQRegion[p], "-")) + totalOnsetDelay + regionStart[] = NumberFromList(0, oodDAQRegion[p], sep = "-") + totalOnsetDelay + regionEnd[] = NumberFromList(1, oodDAQRegion[p], sep = "-") + totalOnsetDelay for(i = 0; i < numEntries; i += 1) first = ScaleToIndex(data, regionStart[i], ROWS) diff --git a/Packages/MIES/MIES_Browser_Plotter.ipf b/Packages/MIES/MIES_Browser_Plotter.ipf index 56fd182a05..653a5bc182 100644 --- a/Packages/MIES/MIES_Browser_Plotter.ipf +++ b/Packages/MIES/MIES_Browser_Plotter.ipf @@ -48,8 +48,8 @@ static Function [string oodDAQRegionsAll, variable totalXRange] GetOodDAQFullRan range = StringFromList(j, entry) oodDAQRegionsAll = AddListItem(range, oodDAQRegionsAll, ";", Inf) - xRangeStart = str2num(StringFromList(0, range, "-")) - xRangeEnd = str2num(StringFromList(1, range, "-")) + xRangeStart = NumberFromList(0, range, sep = "-") + xRangeEnd = NumberFromList(1, range, sep = "-") totalXRange += (xRangeEnd - XRangeStart) endfor endfor @@ -410,7 +410,7 @@ Function CreateTiledChannelGraph(string graph, WAVE config, variable sweepNo, WA DEBUGPRINT("Stim set length (manually calculated)", var = stimSetLength) endif - rangeStartDAPoints = delayOnsetUser + delayOnsetAuto + str2num(StringFromList(k, dDAQActiveHeadstageAll)) * (stimSetLength + delaydDAQ) + rangeStartDAPoints = delayOnsetUser + delayOnsetAuto + NumberFromList(k, dDAQActiveHeadstageAll) * (stimSetLength + delaydDAQ) rangeEndDAPoints = rangeStartDAPoints + stimSetLength // initial total x range once, the stimsets have all the same length for dDAQ @@ -420,8 +420,8 @@ Function CreateTiledChannelGraph(string graph, WAVE config, variable sweepNo, WA elseif(oodDAQEnabled) /// @sa GetSweepSettingsTextKeyWave for the format /// we need points here with taking the onset delays into account - xRangeStartMS = str2num(StringFromList(0, StringFromList(k, oodDAQRegionsAll, ";"), "-")) - xRangeEndMS = str2num(StringFromList(1, StringFromList(k, oodDAQRegionsAll, ";"), "-")) + xRangeStartMS = NumberFromList(0, StringFromList(k, oodDAQRegionsAll, ";"), sep = "-") + xRangeEndMS = NumberFromList(1, StringFromList(k, oodDAQRegionsAll, ";"), sep = "-") sprintf str, "begin[ms] = %g, end[ms] = %g", xRangeStartMS, xRangeEndMS DEBUGPRINT(str) diff --git a/Packages/MIES/MIES_Configuration.ipf b/Packages/MIES/MIES_Configuration.ipf index 6fe1780f04..cc06d492d2 100644 --- a/Packages/MIES/MIES_Configuration.ipf +++ b/Packages/MIES/MIES_Configuration.ipf @@ -1286,7 +1286,7 @@ static Function CONF_RestoreControl(string wName, variable restoreMask, variable ctrlTypeName = JSON_GetString(jsonID, jsonPath + EXPCONFIG_FIELD_CTRLTYPE) i = WhichListItem(ctrlTypeName, EXPCONFIG_GUI_CTRLLIST) ASSERT(i != -1, "Read unknown control type: " + ctrlTypeName) - ctrlType = str2num(StringFromList(i, EXPCONFIG_GUI_CTRLTYPES)) + ctrlType = NumberFromList(i, EXPCONFIG_GUI_CTRLTYPES) ControlInfo/W=$wName $ctrlName ASSERT(abs(V_Flag) == ctrlType, "Expected control of type " + ctrlTypeName + " in window " + wName) else @@ -1796,16 +1796,16 @@ static Function CONF_ControlToJSON(string wName, string ctrlName, variable saveM JSON_AddBoolean(jsonID, ctrlPath + EXPCONFIG_FIELD_CTRLVVALUE, V_Value) else if(saveMask & EXPCONFIG_SAVE_ONLY_RELEVANT) - preferCode = str2num(StringFromList(pos, EXPCONFIG_GUI_PREFERRED)) + preferCode = NumberFromList(pos, EXPCONFIG_GUI_PREFERRED) endif if(preferCode == 0) - if(str2num(StringFromList(pos, EXPCONFIG_GUI_VVALUE))) + if(NumberFromList(pos, EXPCONFIG_GUI_VVALUE)) JSON_AddVariable(jsonID, ctrlPath + EXPCONFIG_FIELD_CTRLVVALUE, V_Value) endif - if(str2num(StringFromList(pos, EXPCONFIG_GUI_SVALUE))) + if(NumberFromList(pos, EXPCONFIG_GUI_SVALUE)) JSON_AddString(jsonID, ctrlPath + EXPCONFIG_FIELD_CTRLSVALUE, S_Value) endif - if(str2num(StringFromList(pos, EXPCONFIG_GUI_SDATAFOLDER))) + if(NumberFromList(pos, EXPCONFIG_GUI_SDATAFOLDER)) JSON_AddString(jsonID, ctrlPath + EXPCONFIG_FIELD_CTRLSDF, S_DataFolder) endif elseif(preferCode == 1) @@ -1833,7 +1833,7 @@ static Function CONF_ControlToJSON(string wName, string ctrlName, variable saveM udataPath = ctrlPath + EXPCONFIG_FIELD_CTRLUSERDATA JSON_AddTreeObject(jsonID, udataPath) udataPath = udataPath + "/" - if(!IsEmpty(S_Userdata) && str2num(StringFromList(pos, EXPCONFIG_GUI_SUSERDATA))) + if(!IsEmpty(S_Userdata) && NumberFromList(pos, EXPCONFIG_GUI_SUSERDATA)) JSON_AddString(jsonID, udataPath, S_Userdata) endif WAVE/Z/T udataKeys = GetUserDataKeys(S_recreation) diff --git a/Packages/MIES/MIES_DAC-Hardware.ipf b/Packages/MIES/MIES_DAC-Hardware.ipf index 7b3aaa383f..c9525404e5 100644 --- a/Packages/MIES/MIES_DAC-Hardware.ipf +++ b/Packages/MIES/MIES_DAC-Hardware.ipf @@ -3378,7 +3378,7 @@ Function HW_SU_PrepareAcq(variable deviceId, variable mode, [WAVE/Z data, FUNCRE if(haveTTL) WAVE/T deviceInfo = GetSUDeviceInfo() - amp0Type = str2num(StringFromList(0, deviceInfo[%LISTOFHEADSTAGES])) + amp0Type = NumberFromList(0, deviceInfo[%LISTOFHEADSTAGES]) sprintf encodeInfo, "00%02d-1", amp0Type EnsureLargeEnoughWave(output, indexShouldExist = outIndex) diff --git a/Packages/MIES/MIES_Epochs.ipf b/Packages/MIES/MIES_Epochs.ipf index 88777fa0ce..72c87ed1e8 100644 --- a/Packages/MIES/MIES_Epochs.ipf +++ b/Packages/MIES/MIES_Epochs.ipf @@ -355,10 +355,10 @@ static Function EP_AddEpochsFromOodDAQRegions(WAVE epochWave, variable channel, Make/FREE/N=(numRegions) epochIndexer tags = ReplaceStringByKey(EPOCH_TYPE_KEY, "", "oodDAQ", STIMSETKEYNAME_SEP, EPOCHNAME_SEP) - epochIndexer[] = EP_AddEpoch(epochWave, channel, XOP_CHANNEL_TYPE_DAC, str2num(StringFromList(0, regions[p], "-")) * MILLI_TO_MICRO + stimsetBegin, \ - str2num(StringFromList(1, regions[p], "-")) * MILLI_TO_MICRO + stimsetBegin, \ - ReplaceNumberByKey(EPOCH_OODDAQ_REGION_KEY, tags, p, STIMSETKEYNAME_SEP, EPOCHNAME_SEP), \ - EPOCH_SN_OODAQ + num2str(p), \ + epochIndexer[] = EP_AddEpoch(epochWave, channel, XOP_CHANNEL_TYPE_DAC, NumberFromList(0, regions[p], sep = "-") * MILLI_TO_MICRO + stimsetBegin, \ + NumberFromList(1, regions[p], sep = "-") * MILLI_TO_MICRO + stimsetBegin, \ + ReplaceNumberByKey(EPOCH_OODDAQ_REGION_KEY, tags, p, STIMSETKEYNAME_SEP, EPOCHNAME_SEP), \ + EPOCH_SN_OODAQ + num2str(p), \ 2, lowerLimit = stimsetBegin, upperLimit = stimsetEnd) endif End diff --git a/Packages/MIES/MIES_GuiUtilities.ipf b/Packages/MIES/MIES_GuiUtilities.ipf index 560977872b..d1c3f0556a 100644 --- a/Packages/MIES/MIES_GuiUtilities.ipf +++ b/Packages/MIES/MIES_GuiUtilities.ipf @@ -1843,7 +1843,7 @@ Function Name2ControlType(string ctrlName) if(pos < 0) return NaN endif - return str2num(StringFromList(pos, EXPCONFIG_GUI_CTRLTYPES)) + return NumberFromList(pos, EXPCONFIG_GUI_CTRLTYPES) End /// @brief Checks if a certain window can act as valid host for subwindows diff --git a/Packages/MIES/MIES_MiesUtilities_Recreation.ipf b/Packages/MIES/MIES_MiesUtilities_Recreation.ipf index 2e721b4b61..006e094e7f 100644 --- a/Packages/MIES/MIES_MiesUtilities_Recreation.ipf +++ b/Packages/MIES/MIES_MiesUtilities_Recreation.ipf @@ -244,7 +244,7 @@ static Function [WAVE channelNumbers, WAVE/WAVE existingWaves] GetSingleSweepWav continue endif - channelNumber = str2num(StringFromList(1, NameOfWave(waves[i]), "_")) + channelNumber = NumberFromList(1, NameOfWave(waves[i]), sep = "_") size = DimSize(channelNumbers, ROWS) Redimension/N=(size + 1) channelNumbers, existingWaves diff --git a/Packages/MIES/MIES_MiesUtilities_Sweep.ipf b/Packages/MIES/MIES_MiesUtilities_Sweep.ipf index 3d87798f5a..efdb78120e 100644 --- a/Packages/MIES/MIES_MiesUtilities_Sweep.ipf +++ b/Packages/MIES/MIES_MiesUtilities_Sweep.ipf @@ -538,7 +538,7 @@ threadsafe Function ExtractSweepNumber(string str) str = RemoveEnding(str, WAVE_BACKUP_SUFFIX) numElements = ItemsInList(str, "_") ASSERT_TS(numElements > 1, "Invalid string with sweep number") - sweepNo = str2num(StringFromList(numElements - 1, str, "_")) + sweepNo = NumberFromList(numElements - 1, str, sep = "_") ASSERT_TS(IsValidSweepNumber(sweepNo), "Invalid sweep numer extracted") return sweepNo diff --git a/Packages/MIES/MIES_PressureControl.ipf b/Packages/MIES/MIES_PressureControl.ipf index 934fc941ef..a90f5c1ca8 100644 --- a/Packages/MIES/MIES_PressureControl.ipf +++ b/Packages/MIES/MIES_PressureControl.ipf @@ -479,7 +479,7 @@ static Function P_CloseDevice(string device) j += 1 while(cmpstr("", ListOfHeadstagesUsingDevice) == 0) j = 0 - headStage = str2num(StringFromList(0, ListOfHeadstagesUsingDevice)) + headStage = NumberFromList(0, ListOfHeadstagesUsingDevice) P_CloseDeviceLowLevel(device, DeviceToClose, headstage) endfor End @@ -508,7 +508,7 @@ static Function P_OpenDevice(string mainDevice, string pressureDevice) listOfHeadstageUsingDevice = P_HeadstageUsingDevice(mainDevice, pressureDevice) numEntries = ItemsInList(listOfHeadstageUsingDevice) for(i = 0; i < numEntries; i += 1) - headStage = str2num(StringFromList(i, ListOfHeadstageUsingDevice)) + headStage = NumberFromList(i, ListOfHeadstageUsingDevice) WAVE PressureDataWv = P_GetPressureDataWaveRef(mainDevice) PressureDataWv[headStage][%DAC_DevID] = deviceID PressureDataWv[headStage][%HW_DAC_Type] = hwType @@ -573,7 +573,7 @@ static Function P_CloseDeviceLowLevel(string device, string deviceToClose, varia numHeadstages = ItemsInList(ListOfHeadstageUsingDevice) for(i = 0; i < numHeadstages; i += 1) if(cmpstr("", ListOfHeadstageUsingDevice) != 0) - headStage = str2num(StringFromList(i, ListOfHeadstageUsingDevice)) + headStage = NumberFromList(i, ListOfHeadstageUsingDevice) deviceID = PressureDataWv[headstage][%DAC_DevID] hwType = pressureDataWv[headstage][%HW_DAC_Type] @@ -1868,15 +1868,15 @@ Function P_PressureDisplayHighlite(string device, variable hilite) endif WAVE PressureDataWv = P_GetPressureDataWaveRef(device) - variable Rz = str2num(stringFromList(0, Zero)) - variable Gz = str2num(stringFromList(1, Zero)) - variable Bz = str2num(stringFromList(2, Zero)) - variable Rl = str2num(stringFromList(0, Low)) - variable Gl = str2num(stringFromList(1, Low)) - variable Bl = str2num(stringFromList(2, Low)) - variable Rh = str2num(stringFromList(0, High)) - variable Gh = str2num(stringFromList(1, High)) - variable Bh = str2num(stringFromList(2, High)) + variable Rz = NumberFromList(0, Zero) + variable Gz = NumberFromList(1, Zero) + variable Bz = NumberFromList(2, Zero) + variable Rl = NumberFromList(0, Low) + variable Gl = NumberFromList(1, Low) + variable Bl = NumberFromList(2, Low) + variable Rh = NumberFromList(0, High) + variable Gh = NumberFromList(1, High) + variable Bh = NumberFromList(2, High) string controlName sprintf controlName, "valdisp_DataAcq_P_LED_%d", pressureDataWv[0][%userSelectedHeadStage] diff --git a/Packages/MIES/MIES_SweepFormula_PSX.ipf b/Packages/MIES/MIES_SweepFormula_PSX.ipf index 1320e83cc9..aa1e3ae9a4 100644 --- a/Packages/MIES/MIES_SweepFormula_PSX.ipf +++ b/Packages/MIES/MIES_SweepFormula_PSX.ipf @@ -3292,7 +3292,7 @@ static Function PSX_RestoreGuiState(string win) selectedBlock = JSON_GetVariable(jsonID, "/specialEventPanel/popup_block") allBlocks = PSX_GetAllEventBlockNumbers(specialEventPanel) - lastBlock = str2num(StringFromList(ItemsInList(allBlocks) - 1, allBlocks)) + lastBlock = NumberFromList(ItemsInList(allBlocks) - 1, allBlocks) PGC_SetAndActivateControl(specialEventPanel, "popup_block", val = limit(selectedBlock, 0, lastBlock)) WAVE/T popups = PSX_GetSpecialEventPanelPopups(specialEventPanel) diff --git a/Packages/MIES/MIES_Utilities_Conversions.ipf b/Packages/MIES/MIES_Utilities_Conversions.ipf index aedae7b30d..d1badf223f 100644 --- a/Packages/MIES/MIES_Utilities_Conversions.ipf +++ b/Packages/MIES/MIES_Utilities_Conversions.ipf @@ -381,7 +381,7 @@ threadsafe Function/WAVE ListToNumericWave(string list, string sep, [variable ty if(ignoreErr) MultiThread wv = str2numSafe(StringFromList(p, list, sep)) else - MultiThread wv = str2num(StringFromList(p, list, sep)) + MultiThread wv = NumberFromList(p, list, sep = sep) endif return wv diff --git a/Packages/MIES/MIES_Utilities_GUI.ipf b/Packages/MIES/MIES_Utilities_GUI.ipf index aa6e272310..0e3714a646 100644 --- a/Packages/MIES/MIES_Utilities_GUI.ipf +++ b/Packages/MIES/MIES_Utilities_GUI.ipf @@ -242,7 +242,7 @@ Function GetNumFromModifyStr(string info, string key, string listChar, variable return str2num(list) else ASSERT(item >= 0 && item < ItemsInList(list, ","), "Invalid index") - return str2num(StringFromList(item, list, ",")) + return NumberFromList(item, list, sep = ",") endif End diff --git a/Packages/MIES/MIES_Utilities_List.ipf b/Packages/MIES/MIES_Utilities_List.ipf index 165eaa817f..2c1cbf843b 100644 --- a/Packages/MIES/MIES_Utilities_List.ipf +++ b/Packages/MIES/MIES_Utilities_List.ipf @@ -219,3 +219,13 @@ Function/S PrepareListForDisplay(string list) return list End + +/// @brief Return the nth list element as number +threadsafe Function NumberFromList(variable index, string list, [string sep]) + + if(ParamIsDefault(sep)) + sep = ";" + endif + + return str2num(StringFromList(index, list, sep)) +End diff --git a/Packages/MIES/MIES_WaveBuilder.ipf b/Packages/MIES/MIES_WaveBuilder.ipf index 8cee6e6665..4c58ed9670 100644 --- a/Packages/MIES/MIES_WaveBuilder.ipf +++ b/Packages/MIES/MIES_WaveBuilder.ipf @@ -803,7 +803,7 @@ static Function/WAVE WB_MakeWaveBuilderWave(WAVE WP, WAVE/T WPT, WAVE SegWvType, params.numberOfPulses = WP[45][i][type] params.trigFuncType = WP[53][i][type] params.noiseType = WP[54][i][type] - params.buildResolution = str2num(StringFromList(WP[55][i][type], WBP_GetNoiseBuildResolution())) + params.buildResolution = NumberFromList(WP[55][i][type], WBP_GetNoiseBuildResolution()) params.pulseType = WP[56][i][type] params.mixedFreq = WP[41][i][type] params.mixedFreqShuffle = WP[42][i][type] diff --git a/Packages/tests/Basic/UTF_Utils_List.ipf b/Packages/tests/Basic/UTF_Utils_List.ipf index d54d87f1eb..2d0813de4e 100644 --- a/Packages/tests/Basic/UTF_Utils_List.ipf +++ b/Packages/tests/Basic/UTF_Utils_List.ipf @@ -113,3 +113,27 @@ Function PrepareListForDisplay_Works() CHECK_EQUAL_STR("a;b", PrepareListForDisplay("a;b")) CHECK_EQUAL_STR("a\rb", PrepareListForDisplay("a;b;")) End + +Function NumberFromList_Works() + + variable num + + num = NumberFromList(0, "") + CHECK_EQUAL_VAR(num, NaN) + + num = NumberFromList(0, "123") + CHECK_EQUAL_VAR(num, 123) + + num = NumberFromList(1, "123;456") + CHECK_EQUAL_VAR(num, 456) + + num = NumberFromList(2, "123;456") + CHECK_EQUAL_VAR(num, NaN) + + // cuts off at first non-digit + num = NumberFromList(1, "123;456a") + CHECK_EQUAL_VAR(num, 456) + + num = NumberFromList(1, "123|456", sep = "|") + CHECK_EQUAL_VAR(num, 456) +End