This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-9041-7f9bdf34ff1337510c73ed4ea8bb904339546239 in repository https://gitbox.apache.org/repos/asf/texera.git
commit bf6bf9fc3a75f540deb6ad2392af32bcbca51295 Author: Eugene Gu <[email protected]> AuthorDate: Fri Nov 20 23:29:27 2026 +0000 feat(workflow-operator): volcano plot significance cut-offs (#9041) ### What changes were proposed in this PR? The Volcano Plot draws a plain scatter of effect size vs `-log10(p)`, colored by effect size, which is already the x axis. Nothing shows where the significance bar is or which points clear it. This PR adds three optional properties, all off by default, so saved workflows render as before. | Property | Type | Effect when set | |---|---|---| | Significance threshold | number, `0 < x <= 1` | dashed horizontal line at `-log10(threshold)` | | Effect-size threshold | number, `> 0` | dashed vertical lines at `+threshold` and `-threshold` | | Color by significance | boolean | with both thresholds set, points are colored `Up` / `Down` / `Not significant` with a legend instead of the continuous `RdBu` scale | ``` Before: px.scatter(color=<effect column>, color_continuous_scale="RdBu") After: same by default + fig.add_hline(...) when a significance threshold is set + fig.add_vline(+eff) / fig.add_vline(-eff) when an effect-size threshold is set + color=<category column>, 3-color map when the toggle is on and both thresholds are set ``` Categories use strict comparisons on the selected p-value column: `Up` = `p < sig` and `effect > eff`, `Down` = `p < sig` and `effect < -eff`, everything else `Not significant`. The categories are stored in a new column that px colors by. It is named `Significance`, with underscores appended until no input column has that name, so no input column is overwritten. The legend and the hover call it `Significance`. The one exception is an effect column that is itself named `Significance`: the category then shows under the column's own name (`Significance_`), because px merges two hover lines that share a label. Colors are ColorBrewer RdBu end points `#B2182B` / `#2166AC` plus grey `#9E9E9E`. Validation: the JSON schema bounds above stop bad values in the property panel. For hand-edited JSON the operator also ignores a threshold it cannot draw (NaN, infinite, <= 0, or a p cut-off > 1) and falls back to the continuous coloring if the toggle is on but a threshold is missing. A cut-off of exactly 1 is allowed and draws its line at y = 0. If the cut-off's `-log10` is beyond the data range the line may fall outside the auto-ranged axis; extending the range is left for a follow-up. Since #8504 merged, the operator also exports a standalone script through `generateStandaloneCode()`. This PR adds the cut-off lines and the `Up` / `Down` / `Not significant` coloring there too, from the same helpers where possible: the color map and category order are one shared literal, the threshold lines come from the same builder, and only the column names differ (Python literals instead of decoded strings). A spec checks that both paths make the same line and coloring decisions for eight threshold shapes, so the exported figure cannot drift from the engine's. The label fix (#9037) and the hover column (#9038) have landed and this branch is merged with both. The title and the axis labels apply in both coloring modes; the color bar title only shows with the continuous coloring, since the category coloring has a legend instead of a color bar. Figures below are the operator's generated Python from `main` and from this branch, run on the 430-gene example workflow from the issue, with thresholds 0.05 and 1. | Before | After (lines only) | After (lines + Color by significance) | |---|---|---| | <img width="1398" height="849" alt="before-main" src="https://github.com/user-attachments/assets/2dd6c551-04b7-41cc-988d-cb19180703cd" /> | <img width="1398" height="849" alt="thresholds-after-lines-only" src="https://github.com/user-attachments/assets/ab3f4d36-3c8f-4073-8f73-ea967bf19174" /> | <img width="1398" height="849" alt="thresholds-after-colored" src="https://github.com/user-attachments/assets/d714fcc7-09cc-4e3f-897d-0da61bb1f8a6" /> | ### Any related issues, documentation, discussions? Closes #9039 PR #8504 (merged) added the `generateStandaloneCode()` export path; the mirroring described above is the follow-up it needed. ### How was this PR tested? New tests in `VolcanoPlotOpDescSpec`, written before the source change: - defaults: no lines, continuous coloring; each threshold alone emits only its own line(s); both with the toggle off keep the continuous coloring - toggle on with both thresholds: strict comparisons, `np.select` categories written to a column whose name no input column has, `color`, `labels` and `category_orders` keyed by that column, exact color map, no `color_continuous_scale` - an effect column named `Significance`: neither path assigns `table["Significance"]`, and both leave the `Significance` label off so the two hover lines stay apart; an effect column named `Significance_` keeps the label - toggle on with one or no threshold: continuous fallback - zero / negative thresholds and a p cut-off above 1 emit no line of their own; `Infinity`, `-Infinity`, `NaN` from JSON never reach the Python - a cut-off of exactly 1 draws its line and still colors by category - thresholds are emitted as valid Python floats, checked for `1e-300`, `Double.MIN_VALUE` and `1e21` in every site they appear - the generated JSON schema carries `exclusiveMinimum` / `maximum` on the two thresholds - JSON deserialization: numbers, numeric strings, null, blank, absent, and the boolean toggle - a workflow JSON saved before this change generates byte-for-byte the same code as a default-constructed operator - `generateStandaloneCode`: defaults draw no line and keep the continuous coloring; the toggle on colors `Up` / `Down` / `Not significant` with the column names as literals; a cross-path test checks that the exported script makes the same cut-off and coloring decisions as the engine for eight threshold shapes (unset, one side only, zero, p cut-off above 1, non-finite, exactly 1) ``` sbt -Dsbt.server.forcestart=true "WorkflowOperator/testOnly *VolcanoPlotOpDescSpec" # Tests: succeeded 43, failed 0, canceled 0, ignored 0, pending 1 (the pending one is pre-existing; the count includes the label and hover tests now on main) sbt -Dsbt.server.forcestart=true "WorkflowOperator/scalafmtAll" "WorkflowOperator/Test/scalafmtAll" sbt -Dsbt.server.forcestart=true "WorkflowOperator/scalafixAll --check" # success ``` Smoke run outside Texera (pytexera stubbed, pandas + plotly 5.24.1) on the 430-gene mock table, 420 rows after the operator drops zero and missing p-values: | Configuration | Lines | Traces | |---|---|---| | defaults | none | 1 continuous trace, 420 points | | 0.05 / 1, toggle off | hline y = 1.301, vlines x = ±1 | 1 continuous trace | | 0.05 / 1, toggle on | same | `Up` 35, `Down` 35, `Not significant` 350 | | 0.05 only, toggle on | hline only | continuous (fallback) | | 0.05 / -1, toggle on | hline only | continuous (fallback) | | 2 / 1, toggle on | vlines only | continuous (fallback) | | `"Infinity"` / `"NaN"` from JSON | none | continuous, no such token in the code | | 1e-300 / 1e21, toggle on | hline y = 300, vlines x = ±1e21 | `Not significant` 420 | | 1 / 1, toggle on | hline y = 0, vlines x = ±1 | `Up` 41, `Down` 38, `Not significant` 341 | Column-name collisions, run on both generated paths with thresholds 0.05 / 1 and the toggle on (10-row table, 8 rows after the filter): an effect column, a selected hover column or the first column named `color`; an unused column, the hover column or the p-value column named `Significance`; `Significance` and `Significance_` both present; an effect column named `Significance_`; a column name with quotes, a backslash and non-ASCII text. Every case draws `Up` 2 / `Down` 1 / `Not significant` 5 in the three colors with the legend titled `Significance`, the x values and the hover text equal the input columns, the engine and exported figures are equal as JSON, and the input table is unchanged. With the effect column named `Significance` the legend reads `Significance_` and the hover keeps both the category line and the x line. For an ordinary table the figure is unchanged from the previous revision of this PR. With thresholds 0.05 / 1, a hover column, and Title, X Label and Y Label all set, both paths draw the configured title and axis labels. With the toggle on they show the `Significance` legend and no trace uses the color bar; with the toggle off the color bar is titled with the X Label. The exponent-form dumps pass `python -m py_compile`. The exported script for the default, lines-only and colored configurations parses with `ast.parse`. ### Was this PR authored or co-authored using generative AI tooling? Yes. Generated-by: Claude Code (claude-fable-5-1). Co-authored with Claude; the author reviewed the change before submission. --- .../volcanoPlot/VolcanoPlotOpDesc.scala | 142 ++++++++- .../volcanoPlot/VolcanoPlotOpDescSpec.scala | 317 ++++++++++++++++++++- 2 files changed, 452 insertions(+), 7 deletions(-) diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDesc.scala index 8ace6e5677..1974307289 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDesc.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDesc.scala @@ -20,9 +20,10 @@ package org.apache.texera.amber.operator.visualization.volcanoPlot import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} +import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.kjetland.jackson.jsonSchema.annotations.{JsonSchemaInject, JsonSchemaTitle} import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext -import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString +import org.apache.texera.amber.pybuilder.PyStringTypes.{EncodableString, PythonLiteral} import org.apache.texera.amber.operator.visualization.PlotlyStandaloneCode import org.apache.texera.amber.operator.visualization.PythonVisualizationOpDesc import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn} @@ -76,6 +77,34 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC @AutofillAttributeName var hoverName: EncodableString = "" + // Numeric: both are compared against the data and the first is log10'd. contentAs + // names the boxed class — Option erases its element type, and a blank must not read as 0. + @JsonProperty(required = false) + @JsonSchemaTitle("Significance threshold") + @JsonPropertyDescription( + "P-value cut-off such as 0.05; draws a dashed horizontal line at -log10 of it" + ) + @JsonSchemaInject(json = """{"exclusiveMinimum": 0, "maximum": 1}""") + @JsonDeserialize(contentAs = classOf[java.lang.Double]) + var significanceThreshold: Option[Double] = None + + @JsonProperty(required = false) + @JsonSchemaTitle("Effect-size threshold") + @JsonPropertyDescription( + "Effect-size cut-off such as 1; draws dashed vertical lines at plus and minus it" + ) + @JsonSchemaInject(json = """{"exclusiveMinimum": 0}""") + @JsonDeserialize(contentAs = classOf[java.lang.Double]) + var effectSizeThreshold: Option[Double] = None + + @JsonProperty(required = false, defaultValue = "false") + @JsonSchemaTitle("Color by significance") + @JsonPropertyDescription( + "Color points Up, Down or Not significant by both thresholds instead of by effect size; " + + "needs both thresholds, otherwise the effect-size coloring is kept" + ) + var colorBySignificance: Boolean = false + @JsonProperty(required = false) @JsonSchemaTitle("Title") @JsonPropertyDescription("The figure title; leave empty for Volcano Plot") @@ -133,6 +162,106 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC |""".stripMargin else "" + /** A cut-off is used only when it can be drawn: finite, above zero and, for the + * p-value, at most one. Anything else counts as unset. The schema bounds keep such + * values out of the property panel; this keeps a hand-edited workflow from failing. + */ + private def isUsable(cutoff: Double): Boolean = + !cutoff.isNaN && !cutoff.isInfinite && cutoff > 0 + + private def significanceCutoff: Option[Double] = + significanceThreshold.filter(sig => isUsable(sig) && sig <= 1) + + private def effectCutoff: Option[Double] = effectSizeThreshold.filter(isUsable) + + /** Both cut-offs are needed to call a point Up or Down. */ + private def colorByCategory: Boolean = + colorBySignificance && significanceCutoff.isDefined && effectCutoff.isDefined + + /** Strict comparisons: a point exactly on a cut-off is not significant. The categories + * go to a new column, Significance with underscores appended until no column has that + * name, so no input column is overwritten. px reads it by name: an array passed as + * `color` is named "color" by px and clashes with a column of that name that x, y or + * hover_name uses. + */ + private def significanceCategories: PythonTemplateBuilder = + if (colorByCategory) { + val sig = significanceCutoff.get + val eff = effectCutoff.get + pyb"""significant = table[$pvalueColumn] < $sig + | significance_column = "Significance" + | while significance_column in table.columns: + | significance_column += "_" + | table[significance_column] = np.select( + | [significant & (table[$effectColumn] > $eff), significant & (table[$effectColumn] < -$eff)], + | ["Up", "Down"], + | default="Not significant", + | ) + |""" + } else pyb"" + + // ColorBrewer RdBu end points plus a neutral grey, so the figure reads like other + // volcano-plot tooling; the continuous RdBu scale stays when not coloring by category. + // Shared by both code paths so the exported figure cannot drift from the engine's. + // The legend and hover call the category column Significance, except when the effect + // column has that name: px would merge the two hover lines that share the label. + private def categoryColorArgs: PythonLiteral = { + val label = + if (effectColumn == "Significance") Seq.empty + else Seq("""labels={significance_column: "Significance"},""") + (Seq("color=significance_column,") ++ label ++ Seq( + """color_discrete_map={"Up": "#B2182B", "Down": "#2166AC", "Not significant": "#9E9E9E"},""", + """category_orders={significance_column: ["Up", "Down", "Not significant"]},""" + )).mkString("\n ") + } + + private def colorArgs: PythonTemplateBuilder = + if (colorByCategory) pyb"$categoryColorArgs" + else + pyb"""color=$effectColumn, + | color_continuous_scale="RdBu",""" + + // The exported script's twins of the two helpers above: the same Python with the + // column names as literals rather than decoded, and the very same threshold literals. + private def standaloneSignificanceCategories: String = + if (colorByCategory) { + val sig = significanceCutoff.get + val eff = effectCutoff.get + val pvalueLit = pyStringLiteral(pvalueColumn) + val effectLit = pyStringLiteral(effectColumn) + s"""significant = table[$pvalueLit] < $sig + | significance_column = "Significance" + | while significance_column in table.columns: + | significance_column += "_" + | table[significance_column] = np.select( + | [significant & (table[$effectLit] > $eff), significant & (table[$effectLit] < -$eff)], + | ["Up", "Down"], + | default="Not significant", + | ) + |""".stripMargin + } else "" + + private def standaloneColorArgs: String = + if (colorByCategory) categoryColorArgs + else + s"""color=${pyStringLiteral(effectColumn)}, + | color_continuous_scale="RdBu",""".stripMargin + + /** One dashed line per cut-off that is set: horizontal at -log10 of the p-value + * cut-off, vertical at plus and minus the effect-size cut-off. + */ + private def thresholdLines: PythonLiteral = { + val lines = scala.collection.mutable.ArrayBuffer[PythonTemplateBuilder]() + significanceCutoff.foreach { sig => + lines += pyb"""fig.add_hline(y=-np.log10($sig), line_dash="dash", line_color="gray")""" + } + effectCutoff.foreach { eff => + lines += pyb"""fig.add_vline(x=$eff, line_dash="dash", line_color="gray")""" + lines += pyb"""fig.add_vline(x=-$eff, line_dash="dash", line_color="gray")""" + } + lines.mkString("\n ") + } + /** The y column is named after the selected p-value column, so a figure drawn * from an adjusted column says so instead of always reading "-log10(pvalue)". */ @@ -172,14 +301,13 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC | return | | table[$yColumn] = -np.log10(table[$pvalueColumn]) - | + | $significanceCategories | fig = px.scatter( | table, | x=$effectColumn, | y=$yColumn, | hover_name=$hoverColumn, - | color=$effectColumn, - | color_continuous_scale="RdBu", + | $colorArgs | title=$titleText | ) | fig.update_layout( @@ -187,6 +315,7 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC | yaxis_title=$yAxisTitle, | coloraxis_colorbar_title_text=$xAxisTitle | ) + | $thresholdLines | | html = plotly.io.to_html(fig, include_plotlyjs='cdn', auto_play=False) | yield {"html-content": html} @@ -217,13 +346,13 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC | output.write(render_error("No rows with valid p-values.")) | else: | table[$yLit] = -np.log10(table[$pvalueLit]) + | $standaloneSignificanceCategories | fig = px.scatter( | table, | x=$effectLit, | y=$yLit, | hover_name=$standaloneHoverColumn, - | color=$effectLit, - | color_continuous_scale="RdBu", + | $standaloneColorArgs | title=${pyStringLiteral(titleText)} | ) | fig.update_layout( @@ -231,6 +360,7 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC | yaxis_title=${pyStringLiteral(yAxisTitle)}, | coloraxis_colorbar_title_text=${pyStringLiteral(xAxisTitle)} | ) + | $thresholdLines | fig.write_json(outputJson) | fig.write_html(outputHtml) | print("Volcano plot saved to " + outputHtml)""".stripMargin diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDescSpec.scala index ab20b4a2c9..a31641bd50 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/visualization/volcanoPlot/VolcanoPlotOpDescSpec.scala @@ -21,7 +21,7 @@ package org.apache.texera.amber.operator.visualization.volcanoPlot import org.apache.texera.amber.core.tuple.AttributeType import org.apache.texera.amber.operator.LogicalOp -import org.apache.texera.amber.operator.metadata.OperatorGroupConstants +import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorMetadataGenerator} import org.apache.texera.amber.util.JSONUtils.objectMapper import org.scalatest.flatspec.AnyFlatSpec import org.apache.texera.amber.operator.GeneratedCodeAssertionChecks @@ -35,6 +35,30 @@ class VolcanoPlotOpDescSpec extends AnyFlatSpec with GeneratedCodeAssertionCheck op } + private val hline = """fig.add_hline(y=-np.log10(0.05), line_dash="dash", line_color="gray")""" + private val vlines = + """fig.add_vline(x=1.0, line_dash="dash", line_color="gray") + | fig.add_vline(x=-1.0, line_dash="dash", line_color="gray")""".stripMargin + private val discreteMap = + """color_discrete_map={"Up": "#B2182B", "Down": "#2166AC", "Not significant": "#9E9E9E"}""" + // The categories go to a column no input column is named after, and px reads it by name. + private val categoryColumn = + """significance_column = "Significance" + | while significance_column in table.columns: + | significance_column += "_" + | table[significance_column] = np.select(""".stripMargin + private val categoryLabel = """labels={significance_column: "Significance"},""" + private val categoryOrders = + """category_orders={significance_column: ["Up", "Down", "Not significant"]},""" + + private def assertContinuousColoring(code: String): Unit = { + code should include("""color_continuous_scale="RdBu"""") + code should not include "np.select" + code should not include "Significance" + code should not include discreteMap + code should not include "category_orders" + } + // Every UI-provided string reaches the generated code as this runtime // decode expression, never as a literal. private def decoded(s: String): String = @@ -121,6 +145,176 @@ class VolcanoPlotOpDescSpec extends AnyFlatSpec with GeneratedCodeAssertionCheck code should not include "HOVER_SENT" } + it should "draw no cut-off line and keep the continuous effect-size coloring by default" in { + val code = configured.generatePythonCode() + code should not include "add_hline" + code should not include "add_vline" + assertContinuousColoring(code) + } + + it should "draw both cut-offs while keeping the continuous coloring when the toggle is off" in { + val op = configured + op.significanceThreshold = Some(0.05) + op.effectSizeThreshold = Some(1.0) + val code = op.generatePythonCode() + code should include(hline) + code should include(vlines) + assertContinuousColoring(code) + } + + it should "skip a significance threshold that has no -log10 (zero or negative)" in { + Seq(0.0, -0.01).foreach { bad => + val op = configured + op.significanceThreshold = Some(bad) + op.effectSizeThreshold = Some(1.0) + op.colorBySignificance = true + val code = op.generatePythonCode() + code should not include "add_hline" + code should not include "np.log10(0.0)" + code should not include "np.log10(-0.01)" + // The effect-size lines still draw, and without a usable p-value cut-off + // no point can be called Up or Down, so the coloring stays continuous. + code should include(vlines) + assertContinuousColoring(code) + } + } + + it should "skip an effect-size threshold that is zero or negative" in { + // A negative cut-off would flip the Up and Down comparisons and call small + // effects significant; zero is the same boundary and is ruled out too. + Seq(0.0, -1.0).foreach { bad => + val op = configured + op.significanceThreshold = Some(0.05) + op.effectSizeThreshold = Some(bad) + op.colorBySignificance = true + val code = op.generatePythonCode() + code should not include "add_vline" + code should include(hline) + assertContinuousColoring(code) + } + } + + it should "skip a significance threshold above one, whose line would sit below the axis" in { + val op = configured + op.significanceThreshold = Some(2.0) + op.effectSizeThreshold = Some(1.0) + op.colorBySignificance = true + val code = op.generatePythonCode() + code should not include "add_hline" + code should include(vlines) + assertContinuousColoring(code) + } + + it should "color Up, Down and Not significant when both thresholds are set and the toggle is on" in { + val op = configured + op.significanceThreshold = Some(0.05) + op.effectSizeThreshold = Some(1.0) + op.colorBySignificance = true + val code = op.generatePythonCode() + // Strict comparisons on the selected p-value column and the effect column: + // a point exactly on a cut-off is not significant. + code should include("significant = table[self.decode_python_template(") + code should include("] < 0.05") + code should include("] > 1.0)") + code should include("] < -1.0)") + code should include(categoryColumn) + code should include("""["Up", "Down"],""") + code should include("""default="Not significant",""") + // The column's name, not an array, which px would name "color". + code should include("color=significance_column,") + code should include(categoryLabel) + code should include(discreteMap) + code should include(categoryOrders) + code should not include "color_continuous_scale" + code should include(hline) + code should include(vlines) + } + + it should "keep an effect column named Significance apart from the categories, in the exported script too" in { + // Writing the categories into table["Significance"] would overwrite an input column + // of that name before px reads the table; as the effect column, the x axis would + // show the category labels. Labelling the category column Significance as well would + // give two hover lines one label, and px would show only one of them. + val op = configured + op.effectColumn = "Significance" + op.significanceThreshold = Some(0.05) + op.effectSizeThreshold = Some(1.0) + op.colorBySignificance = true + Seq(op.generatePythonCode(), op.generateStandaloneCode()).foreach { code => + code should not include """table["Significance"] =""" + code should include(categoryColumn) + code should include("color=significance_column,") + code should not include "labels=" + code should include(discreteMap) + code should include(categoryOrders) + } + // Only that exact name drops the label. + op.effectColumn = "Significance_" + op.generateStandaloneCode() should include(categoryLabel) + } + + it should "accept a significance threshold of exactly one, the schema maximum, and draw it at y = 0" in { + val op = configured + op.significanceThreshold = Some(1.0) + op.effectSizeThreshold = Some(1.0) + op.colorBySignificance = true + val code = op.generatePythonCode() + code should include("""fig.add_hline(y=-np.log10(1.0), line_dash="dash", line_color="gray")""") + code should include(vlines) + code should include("color=significance_column,") + code should include("] < 1.0") + } + + it should "emit every threshold as a literal Python float, exponent forms included" in { + val pyFloat = """^-?\d+(\.\d+)?([eE][-+]?\d+)?$""".r + // The literal after np.log10( or add_vline(x= ; the y column's np.log10(table[...]) does + // not match because its argument does not start with a digit. + val emittedLiterals = """np\.log10\(([0-9.Ee+-]+)\)|add_vline\(x=-?([0-9.Ee+-]+),""".r + Seq( + (1e-300, 1e21, "1.0E-300", "1.0E21"), + (java.lang.Double.MIN_VALUE, 1.0, "4.9E-324", "1.0") + ).foreach { + case (sig, eff, sigLit, effLit) => + val op = configured + op.significanceThreshold = Some(sig) + op.effectSizeThreshold = Some(eff) + op.colorBySignificance = true + val code = op.generatePythonCode() + val literals = emittedLiterals + .findAllMatchIn(code) + .map(m => Option(m.group(1)).getOrElse(m.group(2))) + .toSeq + literals should contain theSameElementsAs Seq(sigLit, effLit, effLit) + literals.foreach(lit => pyFloat.findFirstIn(lit) shouldBe Some(lit)) + // The category comparisons use the very same literals. + code should include(s"] < $sigLit") + code should include(s"] > $effLit)") + code should include(s"] < -$effLit)") + } + } + + it should "fall back to the continuous coloring when the toggle is on but a threshold is missing" in { + val onlySignificance = configured + onlySignificance.significanceThreshold = Some(0.05) + onlySignificance.colorBySignificance = true + val sigCode = onlySignificance.generatePythonCode() + sigCode should include(hline) + sigCode should not include "add_vline" + assertContinuousColoring(sigCode) + + val onlyEffect = configured + onlyEffect.effectSizeThreshold = Some(1.0) + onlyEffect.colorBySignificance = true + val effCode = onlyEffect.generatePythonCode() + effCode should include(vlines) + effCode should not include "add_hline" + assertContinuousColoring(effCode) + + val neither = configured + neither.colorBySignificance = true + assertContinuousColoring(neither.generatePythonCode()) + } + it should "name the y column and its axis after the selected p-value column" in { // The y axis used to read "-log10(pvalue)" whatever column was chosen; a // figure drawn from an adjusted column must say so. @@ -242,6 +436,61 @@ class VolcanoPlotOpDescSpec extends AnyFlatSpec with GeneratedCodeAssertionCheck } } + it should "draw no cut-off line and keep the continuous effect-size coloring by default" in { + val code = configured.generateStandaloneCode() + code should not include "add_hline" + code should not include "add_vline" + assertContinuousColoring(code) + code should include("""color="log2fc",""") + } + + it should "color Up, Down and Not significant with the column names as literals" in { + val op = configured + op.significanceThreshold = Some(0.05) + op.effectSizeThreshold = Some(1.0) + op.colorBySignificance = true + val code = op.generateStandaloneCode() + code should include("""significant = table["pvalue"] < 0.05""") + code should include( + """[significant & (table["log2fc"] > 1.0), significant & (table["log2fc"] < -1.0)]""" + ) + code should include(discreteMap) + code should not include """color_continuous_scale="RdBu"""" + code should include(hline) + code should include(vlines) + code should not include "decode_python_template" + } + + it should "make the same cut-off and coloring decisions as the engine" in { + // The export is drawn from the same figure, so for every shape a stored + // threshold can take the two paths must agree on which lines to draw and on + // whether and how to color by category; a rule applied on one side only would + // make the exported figure differ from the one shown in the workflow. + val cutOffLine = """fig\.add_[hv]line\([^\n]*""".r + // These lines name no input column, so they read the same in both paths. + val categoryLine = """[^\n]*significance_column[^\n]*""".r + Seq( + (None, None, false), + (Some(0.05), Some(1.0), false), + (Some(0.05), Some(1.0), true), + (Some(0.05), None, true), + (Some(0.0), Some(1.0), true), + (Some(1.5), Some(-1.0), true), + (Some(Double.NaN), Some(Double.PositiveInfinity), true), + (Some(1e-300), Some(1e21), true) + ).foreach { + case (sig, eff, toggle) => + val op = configured + op.significanceThreshold = sig + op.effectSizeThreshold = eff + op.colorBySignificance = toggle + val engine = op.generatePythonCode() + val export = op.generateStandaloneCode() + cutOffLine.findAllIn(export).toSeq shouldBe cutOffLine.findAllIn(engine).toSeq + categoryLine.findAllIn(export).toSeq shouldBe categoryLine.findAllIn(engine).toSeq + } + } + it should "name the y column and its axis after the selected p-value column" in { val op = configured op.pvalueColumn = "padj" @@ -297,6 +546,72 @@ class VolcanoPlotOpDescSpec extends AnyFlatSpec with GeneratedCodeAssertionCheck op.generateStandaloneCode() shouldBe configured.generateStandaloneCode() } + /** Reads the shapes a stored workflow can hold. Without `contentAs` a JSON string + * stays unconverted inside the Option and the first use throws. + */ + private def readOp(json: String): VolcanoPlotOpDesc = + objectMapper + .readValue(s"""{"operatorType":"VolcanoPlot"$json}""", classOf[LogicalOp]) + .asInstanceOf[VolcanoPlotOpDesc] + + "VolcanoPlotOpDesc thresholds" should "deserialize JSON numbers" in { + val op = readOp(""","significanceThreshold":0.05,"effectSizeThreshold":1""") + op.significanceThreshold shouldBe Some(0.05) + op.effectSizeThreshold shouldBe Some(1.0) + } + + it should "deserialize numeric strings" in { + val op = readOp(""","significanceThreshold":"0.05","effectSizeThreshold":"1.5"""") + op.significanceThreshold shouldBe Some(0.05) + op.effectSizeThreshold shouldBe Some(1.5) + } + + it should "read an absent, null or blank value as unset rather than as zero" in { + readOp("").significanceThreshold shouldBe None + readOp("").effectSizeThreshold shouldBe None + val nulls = readOp(""","significanceThreshold":null,"effectSizeThreshold":null""") + nulls.significanceThreshold shouldBe None + nulls.effectSizeThreshold shouldBe None + val blanks = readOp(""","significanceThreshold":"","effectSizeThreshold":""""") + blanks.significanceThreshold shouldBe None + blanks.effectSizeThreshold shouldBe None + } + + it should "treat a non-finite value read from JSON as unset so no Infinity or NaN reaches the Python" in { + Seq(""""Infinity"""", """"-Infinity"""", """"NaN"""").foreach { raw => + val op = readOp( + s""","significanceThreshold":$raw,"effectSizeThreshold":$raw,"colorBySignificance":true""" + ) + // Jackson does read the text as a Double; the operator must be the one to drop it. + op.significanceThreshold.exists(d => d.isNaN || d.isInfinite) shouldBe true + op.effectSizeThreshold.exists(d => d.isNaN || d.isInfinite) shouldBe true + op.effectColumn = "log2fc" + op.pvalueColumn = "pvalue" + val code = op.generatePythonCode() + code should not include "add_hline" + code should not include "add_vline" + code should not include "Infinity" + code should not include "NaN" + assertContinuousColoring(code) + } + } + + "VolcanoPlotOpDesc schema" should "bound both thresholds so the property panel rejects what the operator ignores" in { + val props = OperatorMetadataGenerator + .generateOperatorJsonSchema(classOf[VolcanoPlotOpDesc]) + .path("properties") + props.path("significanceThreshold").path("exclusiveMinimum").asDouble() shouldBe 0.0 + props.path("significanceThreshold").path("maximum").asDouble() shouldBe 1.0 + props.path("effectSizeThreshold").path("exclusiveMinimum").asDouble() shouldBe 0.0 + props.path("effectSizeThreshold").has("maximum") shouldBe false + } + + "VolcanoPlotOpDesc.colorBySignificance" should "default to false and read a saved value" in { + readOp("").colorBySignificance shouldBe false + readOp(""","colorBySignificance":true""").colorBySignificance shouldBe true + readOp(""","colorBySignificance":false""").colorBySignificance shouldBe false + } + "VolcanoPlotOpDesc.generatePythonCode" should "currently render code even when required fields are empty (no assert guard)" in { // Documents the present behavior: VolcanoPlotOpDesc does not assert on // its required fields inside `generatePythonCode`. An empty
