Skip to content

Commit

Permalink
NumberFromList: Add it
Browse files Browse the repository at this point in the history
This is more succinct.
  • Loading branch information
t-b committed Dec 2, 2024
1 parent 2a4b4cf commit d00be91
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions Packages/MIES/MIES_Browser_Plotter.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions Packages/MIES/MIES_Configuration.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_DAC-Hardware.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Packages/MIES/MIES_Epochs.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_GuiUtilities.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_MiesUtilities_Recreation.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_MiesUtilities_Sweep.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions Packages/MIES/MIES_PressureControl.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_SweepFormula_PSX.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_Utilities_Conversions.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_Utilities_GUI.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions Packages/MIES/MIES_Utilities_List.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Packages/MIES/MIES_WaveBuilder.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 24 additions & 0 deletions Packages/tests/Basic/UTF_Utils_List.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d00be91

Please sign in to comment.