Skip to content

Commit de3a849

Browse files
authored
Merge pull request #2423 from AllenInstitute/bugfix/2423-sweepformula-multiple-axis-labels-for-x-axis
SweepFormula: Groups multiple x-axis labels
2 parents 15e3a5e + 74a8ffd commit de3a849

File tree

3 files changed

+118
-50
lines changed

3 files changed

+118
-50
lines changed

Packages/MIES/MIES_SweepFormula.ipf

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,16 +1266,9 @@ static Function [WAVE/WAVE formulaResults, STRUCT SF_PlotMetaData plotMetaData]
12661266
if(WaveExists(wvXRef))
12671267
if(numResultsX == 1)
12681268
WAVE/Z wvXdata = wvXRef[0]
1269-
if(WaveExists(wvXdata) && DimSize(wvXdata, ROWS) == numResultsY && numpnts(wvYdata) == 1)
1270-
if(IsTextWave(wvXdata))
1271-
WAVE/T wT = wvXdata
1272-
Make/FREE/T wvXnewDataT = {wT[i]}
1273-
formulaResults[i][%FORMULAX] = wvXnewDataT
1274-
else
1275-
WAVE wv = wvXdata
1276-
Make/FREE/D wvXnewDataD = {wv[i]}
1277-
formulaResults[i][%FORMULAX] = wvXnewDataD
1278-
endif
1269+
if(WaveExists(wvXdata) && DimSize(wvXdata, ROWS) == numResultsY && numpnts(wvYdata) == 1 && numpnts(wvXdata) != 1)
1270+
Duplicate/FREE/T/RMD=[i] wvXdata, wvXnewData
1271+
formulaResults[i][%FORMULAX] = wvXnewData
12791272
else
12801273
formulaResults[i][%FORMULAX] = wvXRef[0]
12811274
endif
@@ -1302,7 +1295,7 @@ static Function [WAVE/WAVE formulaResults, STRUCT SF_PlotMetaData plotMetaData]
13021295

13031296
dataUnits = ""
13041297
if(!IsNull(dataUnitCheck))
1305-
dataUnits = SelectString(addDataUnitsInAnnotation && !IsEmpty(dataUnitCheck), "", "(" + dataUnitCheck + ")")
1298+
dataUnits = SelectString(addDataUnitsInAnnotation && !IsEmpty(dataUnitCheck), "", SF_FormatUnit(dataUnitCheck))
13061299
endif
13071300

13081301
plotMetaData.dataType = JWN_GetStringFromWaveNote(wvYRef, SF_META_DATATYPE)
@@ -1314,6 +1307,11 @@ static Function [WAVE/WAVE formulaResults, STRUCT SF_PlotMetaData plotMetaData]
13141307
return [formulaResults, plotMetaData]
13151308
End
13161309

1310+
static Function/S SF_FormatUnit(string unit)
1311+
1312+
return "(" + unit + ")"
1313+
End
1314+
13171315
static Function/S SF_GetAnnotationPrefix(string dataType)
13181316

13191317
strswitch(dataType)
@@ -1749,48 +1747,59 @@ static Function SF_CommonWindowSetup(string win, string graph)
17491747
DoWindow/T $win, newTitle
17501748
End
17511749

1752-
static Function/WAVE SF_GatherYUnits(WAVE/WAVE formulaResults, string explicitLbl, WAVE/Z/T yUnits)
1750+
static Function SF_GatherAxisLabels(WAVE/WAVE formulaResults, string explicitLbl, string formulaLabel, WAVE/T axisLabels)
17531751

17541752
variable i, size, numData
1753+
string unit
17551754

1756-
if(!WaveExists(yUnits))
1757-
Make/FREE/T/N=0 yUnits
1758-
endif
1759-
1760-
size = DimSize(yUnits, ROWS)
1755+
size = DimSize(axisLabels, ROWS)
17611756
if(!isEmpty(explicitLbl))
1762-
Redimension/N=(size + 1) yUnits
1763-
yUnits[size] = explicitLbl
1764-
return yUnits
1757+
Redimension/N=(size + 1) axisLabels
1758+
axisLabels[size] = explicitLbl
1759+
return NaN
17651760
endif
17661761

17671762
numData = DimSize(formulaResults, ROWS)
1768-
Redimension/N=(size + numData) yUnits
1763+
Redimension/N=(size + numData) axisLabels
17691764

17701765
for(i = 0; i < numData; i += 1)
17711766
WAVE/Z wvResultY = formulaResults[i][%FORMULAY]
17721767
if(!WaveExists(wvResultY))
17731768
continue
17741769
endif
1775-
yUnits[size] = WaveUnits(wvResultY, COLS)
1776-
size += 1
1770+
1771+
strswitch(formulaLabel)
1772+
case "FORMULAY":
1773+
unit = WaveUnits(wvResultY, COLS)
1774+
break
1775+
case "FORMULAX":
1776+
WAVE/Z wvResultX = formulaResults[i][%FORMULAX]
1777+
if(WaveExists(wvResultX))
1778+
unit = WaveUnits(wvResultX, ROWS)
1779+
else
1780+
unit = WaveUnits(wvResultY, ROWS)
1781+
endif
1782+
break
1783+
default:
1784+
ASSERT(0, "Unsupported formulaLabel: " + formulaLabel)
1785+
break
1786+
endswitch
1787+
1788+
// fallback to the unit if present
1789+
if(!IsEmpty(unit))
1790+
axisLabels[size] = SF_FormatUnit(unit)
1791+
size += 1
1792+
endif
17771793
endfor
1778-
Redimension/N=(size) yUnits
17791794

1780-
return yUnits
1795+
Redimension/N=(size) axisLabels
17811796
End
17821797

1783-
static Function/S SF_CombineYUnits(WAVE/T units)
1784-
1785-
string separator = " / "
1786-
string result = ""
1798+
static Function/S SF_CombineAxisLabels(WAVE/T axisLabels)
17871799

1788-
WAVE/T unique = GetUniqueEntries(units, dontDuplicate = 1)
1789-
for(unit : unique)
1790-
result += unit + separator
1791-
endfor
1800+
WAVE/T unique = GetUniqueEntries(axisLabels, dontDuplicate = 1)
17921801

1793-
return RemoveEndingRegExp(result, separator)
1802+
return TextWaveToList(unique, " / ", trailSep = 0)
17941803
End
17951804

17961805
static Function SF_CheckNumTraces(string graph, variable numTraces)
@@ -1874,7 +1883,7 @@ static Function SF_FormulaPlotter(string graph, string formula, [variable dmMode
18741883
variable winDisplayMode, showLegend, tagCounter, overrideMarker
18751884
variable xMxN, yMxN, xPoints, yPoints, keepUserSelection, numAnnotations, formulasAreDifferent, postPlotPSX
18761885
variable formulaCounter, gdIndex, markerCode, lineCode, lineStyle, traceToFront, isCategoryAxis
1877-
string win, wList, winNameTemplate, exWList, wName, annotation, yAxisLabel, wvName, info, xAxis
1886+
string win, wList, winNameTemplate, exWList, wName, annotation, xAxisLabel, yAxisLabel, wvName, info, xAxis
18781887
string formulasRemain, yAndXFormula, xFormula, yFormula, tagText, name, winHook
18791888
STRUCT SF_PlotMetaData plotMetaData
18801889
STRUCT RGBColor color
@@ -1902,8 +1911,9 @@ static Function SF_FormulaPlotter(string graph, string formula, [variable dmMode
19021911
postPlotPSX = 0
19031912
showLegend = 1
19041913
formulaCounter = 0
1905-
WAVE/Z wvX = $""
1906-
WAVE/Z/T yUnits = $""
1914+
WAVE/Z wvX = $""
1915+
1916+
Make/FREE/T/N=0 xAxisLabels, yAxisLabels
19071917

19081918
formulasRemain = graphCode[j]
19091919

@@ -1935,8 +1945,8 @@ static Function SF_FormulaPlotter(string graph, string formula, [variable dmMode
19351945
Abort
19361946
endtry
19371947

1938-
WAVE/T yUnitsResult = SF_GatherYUnits(formulaResults, plotMetaData.yAxisLabel, yUnits)
1939-
WAVE/T yUnits = yUnitsResult
1948+
SF_GatherAxisLabels(formulaResults, plotMetaData.xAxisLabel, "FORMULAX", xAxisLabels)
1949+
SF_GatherAxisLabels(formulaResults, plotMetaData.yAxisLabel, "FORMULAY", yAxisLabels)
19401950

19411951
if(!cmpstr(plotMetaData.dataType, SF_DATATYPE_PSX))
19421952
PSX_Plot(win, graph, formulaResults, plotMetaData)
@@ -2127,8 +2137,6 @@ static Function SF_FormulaPlotter(string graph, string formula, [variable dmMode
21272137
formulaCounter += 1
21282138
while(1)
21292139

2130-
yAxisLabel = SF_CombineYUnits(yUnits)
2131-
21322140
if(showLegend)
21332141
customLegend = JWN_GetStringFromWaveNote(formulaResults, SF_META_CUSTOM_LEGEND)
21342142

@@ -2268,15 +2276,19 @@ static Function SF_FormulaPlotter(string graph, string formula, [variable dmMode
22682276
endfor
22692277
endfor
22702278

2271-
if(!IsEmpty(plotMetaData.xAxisLabel) && traceCnt > 0)
2272-
Label/W=$win bottom, plotMetaData.xAxisLabel
2273-
ModifyGraph/W=$win tickUnit(bottom)=1
2274-
endif
2275-
if(!IsEmpty(yAxisLabel) && traceCnt > 0)
2276-
Label/W=$win left, yAxisLabel
2277-
ModifyGraph/W=$win tickUnit(left)=1
2278-
endif
22792279
if(traceCnt > 0)
2280+
xAxisLabel = SF_CombineAxisLabels(xAxisLabels)
2281+
if(!IsEmpty(xAxisLabel))
2282+
Label/W=$win bottom, xAxisLabel
2283+
ModifyGraph/W=$win tickUnit(bottom)=1
2284+
endif
2285+
2286+
yAxisLabel = SF_CombineAxisLabels(yAxisLabels)
2287+
if(!IsEmpty(yAxisLabel))
2288+
Label/W=$win left, yAxisLabel
2289+
ModifyGraph/W=$win tickUnit(left)=1
2290+
endif
2291+
22802292
ModifyGraph/W=$win zapTZ(bottom)=1
22812293
endif
22822294

Packages/tests/Basic/UTF_SweepFormula.ipf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,3 +2120,59 @@ static Function TestVariableReadOnly()
21202120

21212121
KillWaves/Z root:testData
21222122
End
2123+
2124+
static Function TestKeepsUnitsWhenMappingMultipleYToOne()
2125+
2126+
string win, graph, xAxis, yAxis, code
2127+
2128+
win = GetDataBrowserWithData()
2129+
graph = SFH_GetFormulaGraphForBrowser(win)
2130+
2131+
Make/O data1 = {1}
2132+
SetScale/P x, 0, 1, "x1", data1
2133+
SetScale/P y, 0, 1, "y1", data1
2134+
2135+
Make/O data2 = {2, 3}
2136+
SetScale/P x, 0, 1, "x2", data2
2137+
SetScale/P y, 0, 1, "y2", data2
2138+
2139+
code = "dataset(wave(data1), wave(data1)) vs dataset(wave(data2))"
2140+
ExecuteSweepFormulaInDB(code, win)
2141+
yAxis = AxisLabel(graph, "left")
2142+
CHECK_EQUAL_STR(yAxis, "(y1)")
2143+
xAxis = AxisLabel(graph, "bottom")
2144+
CHECK_EQUAL_STR(xAxis, "(x2)")
2145+
2146+
KillWaves/Z data1, data2
2147+
End
2148+
2149+
static Function TestAxisLabelGathering()
2150+
2151+
string win, graph, xAxis, yAxis, code
2152+
2153+
win = GetDataBrowserWithData()
2154+
graph = SFH_GetFormulaGraphForBrowser(win)
2155+
2156+
Make/O data1 = {1}
2157+
SetScale/P x, 0, 1, "x1", data1
2158+
SetScale/P y, 0, 1, "y1", data1
2159+
2160+
Make/O data2 = {2}
2161+
SetScale/P x, 0, 1, "x2", data2
2162+
SetScale/P y, 0, 1, "y2", data2
2163+
2164+
Make/O data3 = {3}
2165+
SetScale/P x, 0, 1, "x3", data3
2166+
SetScale/P y, 0, 1, "y3", data3
2167+
2168+
code = "wave(data1)\r" + \
2169+
"with \r" + \
2170+
"wave(data2) vs wave(data3)\r"
2171+
ExecuteSweepFormulaInDB(code, win)
2172+
yAxis = AxisLabel(graph, "left")
2173+
CHECK_EQUAL_STR(yAxis, "(y1) / (y2)")
2174+
xAxis = AxisLabel(graph, "bottom")
2175+
CHECK_EQUAL_STR(xAxis, "(x1) / (x3)")
2176+
2177+
KillWaves/Z data1, data2, data3
2178+
End

Packages/tests/HardwareBasic/UTF_SweepFormulaHardware.ipf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static Function TestSweepFormulaAxisLabels(string device)
136136

137137
// Test combine of different data unit in multiple data waves
138138
str = AxisLabel(plotWin, "left")
139-
strRef = "mV / pA"
139+
strRef = "(mV) / (pA)"
140140
CHECK_EQUAL_STR(strRef, str)
141141

142142
str = AxisLabel(plotWin, "bottom")

0 commit comments

Comments
 (0)