Skip to content

Commit

Permalink
enhance number display
Browse files Browse the repository at this point in the history
  • Loading branch information
Peva Blanchard committed Jul 22, 2023
1 parent b5e8077 commit a542e9a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.kleis.lcaplugin.ui.toolwindow

import ch.kleis.lcaplugin.core.lang.evaluator.ArgumentName
import ch.kleis.lcaplugin.core.math.DualNumber
import ch.kleis.lcaplugin.core.matrix.IndexedCollection
import org.jetbrains.kotlinx.multik.ndarray.operations.toList
import kotlin.math.abs

class DualNumberRenderer(
private val arguments: IndexedCollection<ArgumentName>
) {
private val absoluteSensitivityThreshold = 1e-10
private val relativeToMaxThreshold = 2e-2
private val dot = "\u00B7"
private val delta = "δ"

fun render(v: DualNumber): String {
val z = "${DisplayedNumber(v.zeroth)}"
val f = v.first
.toList()
.mapIndexed { i, d ->
d to arguments[i].uid
}
val maxSensitivity = f.maxOf { abs(it.first) }
val ff = f
.filter { maxSensitivity > absoluteSensitivityThreshold && abs(it.first)/maxSensitivity > relativeToMaxThreshold }
.fold("") { acc, p ->
val d = p.first
val absD = DisplayedNumber(abs(d))
val parameterName = p.second
if (d >= 0.0) "$acc + $absD$dot$delta$parameterName"
else "$acc - $absD$dot$delta$parameterName"
}
return z + ff
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package ch.kleis.lcaplugin.ui.toolwindow

import ch.kleis.lcaplugin.core.assessment.Inventory
import ch.kleis.lcaplugin.core.lang.value.MatrixColumnIndex
import ch.kleis.lcaplugin.core.matrix.ImpactFactorMatrix
import javax.swing.event.TableModelListener
import javax.swing.table.TableModel

class InventoryTableModel(
private val inventory: Inventory,
observablePortComparator: Comparator<MatrixColumnIndex>,
) : TableModel {
private val dualNumberRenderer = DualNumberRenderer(inventory.arguments)
private val sortedObservablePorts = inventory.getObservablePorts().getElements().sortedWith(observablePortComparator)
private val sortedControllablePorts = inventory.getControllablePorts().getElements().sortedBy { it.getUID() }

Expand Down Expand Up @@ -59,15 +59,15 @@ class InventoryTableModel(

val quantity = inventory.supply.quantityOf(outputProduct)
if (columnIndex == 1) {
return DisplayedNumber(quantity.amount).toString()
return dualNumberRenderer.render(quantity.amount)
}
if (columnIndex == 2) {
return "${quantity.unit.symbol}"
}

val inputProduct = sortedControllablePorts[columnIndex - 3]
val ratio = inventory.impactFactors.valueRatio(outputProduct, inputProduct).amount
return DisplayedNumber(quantity.amount * ratio).toString()
return dualNumberRenderer.render(quantity.amount * ratio)
}

override fun setValueAt(aValue: Any?, rowIndex: Int, columnIndex: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ch.kleis.lcaplugin.core.lang.value.BioExchangeValue
import ch.kleis.lcaplugin.core.lang.value.ProcessValue
import ch.kleis.lcaplugin.core.lang.value.SystemValue
import ch.kleis.lcaplugin.core.lang.value.TechnoExchangeValue
import ch.kleis.lcaplugin.core.matrix.IndexedCollection
import org.junit.Assert
import org.junit.Test
import kotlin.test.assertFailsWith
Expand All @@ -30,7 +31,7 @@ class AllocationTest {
fun apply_when_coProducts_should_duplicate_process() {
// Given
val system = SystemValue(
quantitativeArguments = emptyMap(),
quantitativeArguments = IndexedCollection(emptyList()),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -64,7 +65,7 @@ class AllocationTest {
fun apply_when_coProducts_should_keep_only_one_product_per_process() {
// Given
val system = SystemValue(
quantitativeArguments = emptyMap(),
quantitativeArguments = IndexedCollection(emptyList()),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -98,7 +99,7 @@ class AllocationTest {
fun apply_when_allocation_should_divide_inputs_quantities() {
// Given
val system = SystemValue(
quantitativeArguments = emptyMap(),
quantitativeArguments = IndexedCollection(emptyList()),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -138,7 +139,7 @@ class AllocationTest {
fun apply_when_allocation_should_divide_biosphere_quantities() {
// given
val system = SystemValue(
quantitativeArguments = emptyMap(),
quantitativeArguments = IndexedCollection(emptyList()),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -259,7 +260,7 @@ class AllocationTest {
// given
val allocation = Allocation()
val system = SystemValue(
quantitativeArguments = emptyMap(),
quantitativeArguments = IndexedCollection(emptyList()),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -302,7 +303,7 @@ class AllocationTest {
fun apply_shouldKeepAllocation() {
// given
val system = SystemValue(
quantitativeArguments = emptyMap(),
quantitativeArguments = IndexedCollection(emptyList()),
mutableSetOf(ProcessValue("", emptyMap(), listOf(), listOf(), listOf())),
mutableSetOf(propanolCharacterization)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ch.kleis.lcaplugin.core.lang.fixture.ProductValueFixture
import ch.kleis.lcaplugin.core.lang.fixture.QuantityValueFixture
import ch.kleis.lcaplugin.core.lang.fixture.SubstanceCharacterizationValueFixture
import ch.kleis.lcaplugin.core.lang.value.TechnoExchangeValue
import ch.kleis.lcaplugin.core.matrix.IndexedCollection
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand All @@ -24,7 +25,7 @@ class EvaluationTraceTest {
val p3 = ProcessValueFixture.carrotProcessValue
.copy(products = listOf(TechnoExchangeValue(QuantityValueFixture.oneKilogram, product3)))

val trace = EvaluationTrace(emptyMap())
val trace = EvaluationTrace(IndexedCollection(emptyList()))
trace.add(p1)
trace.commit()
trace.add(p2)
Expand All @@ -51,7 +52,7 @@ class EvaluationTraceTest {
val p3 = ProcessValueFixture.carrotProcessValue
.copy(products = listOf(TechnoExchangeValue(QuantityValueFixture.oneKilogram, product3)))

val trace = EvaluationTrace.empty(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))
trace.add(p1)
trace.add(p2)
trace.commit()
Expand All @@ -66,7 +67,7 @@ class EvaluationTraceTest {
@Test
fun trace_getEntryPoint_whenEmptyTrace_shouldThrow() {
// given
val trace = EvaluationTrace.empty(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))

// when/then
val e = assertFailsWith(EvaluatorException::class) { trace.getEntryPoint() }
Expand All @@ -92,7 +93,7 @@ class EvaluationTraceTest {
val sc = SubstanceCharacterizationValueFixture.propanolCharacterization
val substance = sc.referenceExchange.substance

val trace = EvaluationTrace(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))
trace.add(p1)
trace.commit()
trace.add(p2)
Expand Down Expand Up @@ -134,7 +135,7 @@ class EvaluationTraceTest {
val sc = SubstanceCharacterizationValueFixture.propanolCharacterization
val substance = sc.referenceExchange.substance

val trace = EvaluationTrace(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))
trace.add(p1)
trace.commit()
trace.add(p2)
Expand Down Expand Up @@ -163,7 +164,7 @@ class EvaluationTraceTest {
val p1 = ProcessValueFixture.carrotProcessValue
val p2 = p1.copy(name = "another_carrot_production")
val sc = SubstanceCharacterizationValueFixture.propanolCharacterization
val trace = EvaluationTrace(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))

// when
trace.add(p1)
Expand All @@ -182,7 +183,7 @@ class EvaluationTraceTest {
val p1 = ProcessValueFixture.carrotProcessValue
val p2 = p1.copy(name = "another_carrot_production")
val sc = SubstanceCharacterizationValueFixture.propanolCharacterization
val trace = EvaluationTrace(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))
trace.add(p1)
trace.commit()
trace.add(p2)
Expand All @@ -201,7 +202,7 @@ class EvaluationTraceTest {
fun trace_emptyCommit_shouldNotAddStages() {
// given
val p = ProcessValueFixture.carrotProcessValue
val trace = EvaluationTrace(emptyMap())
val trace = EvaluationTrace.empty(IndexedCollection(emptyList()))

// when
trace.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package ch.kleis.lcaplugin.core.lang.fixture
import ch.kleis.lcaplugin.core.lang.fixture.ProcessValueFixture.Companion.carrotProcessValue
import ch.kleis.lcaplugin.core.lang.fixture.ProcessValueFixture.Companion.carrotProcessValueWithAllocation
import ch.kleis.lcaplugin.core.lang.value.SystemValue
import ch.kleis.lcaplugin.core.matrix.IndexedCollection

class SystemValueFixture {
companion object {
fun carrotSystem() = SystemValue(emptyMap(), mutableSetOf(carrotProcessValue), mutableSetOf())
fun carrotSystemWithAllocation() = SystemValue(emptyMap(), mutableSetOf(carrotProcessValueWithAllocation), mutableSetOf())
fun carrotSystem() = SystemValue(IndexedCollection(emptyList()), mutableSetOf(carrotProcessValue), mutableSetOf())
fun carrotSystemWithAllocation() = SystemValue(IndexedCollection(emptyList()), mutableSetOf(carrotProcessValueWithAllocation), mutableSetOf())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.kleis.lcaplugin.ui.toolwindow

import ch.kleis.lcaplugin.core.lang.evaluator.ArgumentName
import ch.kleis.lcaplugin.core.math.DualNumber
import ch.kleis.lcaplugin.core.matrix.IndexedCollection
import org.junit.Test
import kotlin.test.assertEquals


class DualNumberRendererTest {
private val dx = DualNumber.basis(2, 0)
private val dy = DualNumber.basis(2, 1)

@Test
fun test_render() {
// given
val arguments = IndexedCollection(
listOf(
"x", "y"
).map { ArgumentName(it) }
)
val renderer = DualNumberRenderer(arguments)

// when/then
listOf(
DualNumber.constant(123456.123) to "123E3",
DualNumber.constant(123456.123) + dx to "123E3 + 1\u00B7δx",
DualNumber.constant(123456.123) - dy to "123E3 - 1\u00B7δy",
).forEach {
val actual = renderer.render(it.first)
assertEquals(it.second, actual)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class LcaProcessAssessResultTest {
val impactFactorsData = MatrixFixture.makeDualMatrix(2, 2, arrayOf(1.0, 10.0, 1.0, 10.0))
val supplyData = MatrixFixture.makeDualMatrix(1, 2, arrayOf(1.0, 2.0))
val inv = Inventory(
arguments = IndexedCollection(emptyList()),
impactFactors = ImpactFactorMatrix(
observablePorts, IndexedCollection(listOf(substance, product)), impactFactorsData
),
Expand Down

0 comments on commit a542e9a

Please sign in to comment.