From cadfc20e540830279df5fb5cfc53a3a0fd4c1f1a Mon Sep 17 00:00:00 2001 From: darksnake Date: Sun, 5 Dec 2021 17:24:53 +0300 Subject: [PATCH] fix parallel computation for spectrum in viewer --- .../kotlin/hep/dataforge/plots/PlotGroup.kt | 34 +++++++------------ numass-viewer/build.gradle.kts | 2 +- .../kotlin/inr/numass/viewer/AmplitudeView.kt | 14 +++++--- .../inr/numass/viewer/DataController.kt | 6 ++-- .../inr/numass/viewer/DirectoryWatchView.kt | 2 +- .../kotlin/inr/numass/viewer/SpectrumView.kt | 19 +++++------ 6 files changed, 35 insertions(+), 42 deletions(-) diff --git a/dataforge-plots/src/main/kotlin/hep/dataforge/plots/PlotGroup.kt b/dataforge-plots/src/main/kotlin/hep/dataforge/plots/PlotGroup.kt index acf1b202..788742c7 100644 --- a/dataforge-plots/src/main/kotlin/hep/dataforge/plots/PlotGroup.kt +++ b/dataforge-plots/src/main/kotlin/hep/dataforge/plots/PlotGroup.kt @@ -112,36 +112,28 @@ class PlotGroup(override val name: String, descriptor: NodeDescriptor = NodeDes } @ProvidesNames(PLOT_TARGET) - fun list(): Stream { - return stream().map { it.first.toString() } - } + fun list(): Stream = stream().map { it.first.toString() } /** * Recursive stream of all plots excluding intermediate nodes * * @return */ - fun stream(recursive: Boolean = true): Stream> { - return plots.stream().flatMap { - if (recursive && it is PlotGroup) { - it.stream().map { pair -> Pair(Name.ofSingle(it.name) + pair.first, pair.second) } - } else { - Stream.of(Pair(Name.ofSingle(it.name), it)) - } + fun stream(recursive: Boolean = true): Stream> = plots.stream().flatMap { + if (recursive && it is PlotGroup) { + it.stream().map { pair -> Pair(Name.ofSingle(it.name) + pair.first, pair.second) } + } else { + Stream.of(Pair(Name.ofSingle(it.name), it)) } } @Provides(PLOT_TARGET) - operator fun get(name: String): Plottable? { - return get(Name.of(name)) - } + operator fun get(name: String): Plottable? = get(Name.of(name)) - operator fun get(name: Name): Plottable? { - return when { - name.length == 0 -> this - name.length == 1 -> plots.find { it.name == name.unescaped } - else -> (get(name.cutLast()) as? PlotGroup)?.get(name.last) - } + operator fun get(name: Name): Plottable? = when (name.length) { + 0 -> this + 1 -> plots.find { it.name == name.unescaped } + else -> (get(name.cutLast()) as? PlotGroup)?.get(name.last) } /** @@ -203,9 +195,7 @@ class PlotGroup(override val name: String, descriptor: NodeDescriptor = NodeDes * * @return */ - override fun iterator(): Iterator { - return this.plots.iterator() - } + override fun iterator(): Iterator = this.plots.iterator() class Wrapper : hep.dataforge.io.envelopes.Wrapper { diff --git a/numass-viewer/build.gradle.kts b/numass-viewer/build.gradle.kts index ca939a05..2303de8f 100644 --- a/numass-viewer/build.gradle.kts +++ b/numass-viewer/build.gradle.kts @@ -19,7 +19,7 @@ application { mainClass.set("inr.numass.viewer.Viewer") } -version = "0.6.1" +version = "0.6.2" description = "The viewer for numass data" diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt index 5fa0391d..6d581026 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt @@ -12,6 +12,7 @@ import inr.numass.data.analyzers.NumassAnalyzer import inr.numass.data.analyzers.withBinning import javafx.beans.binding.DoubleBinding import javafx.beans.property.SimpleBooleanProperty +import javafx.beans.property.SimpleIntegerProperty import javafx.beans.property.SimpleObjectProperty import javafx.collections.FXCollections import javafx.collections.MapChangeListener @@ -49,17 +50,17 @@ class AmplitudeView : View(title = "Numass amplitude spectrum plot", icon = Imag }.setType() } - val binningProperty = SimpleObjectProperty(20) - var binning by binningProperty + val binningProperty = SimpleIntegerProperty(2) + var binning: Int by binningProperty val normalizeProperty = SimpleBooleanProperty(true) var normalize by normalizeProperty private val plotContainer = PlotContainer(frame).apply { - val binningSelector: ChoiceBox = ChoiceBox(FXCollections.observableArrayList(1, 2, 8, 16, 32, 50)).apply { + val binningSelector: ChoiceBox = ChoiceBox(FXCollections.observableArrayList(1, 2, 8, 16, 32)).apply { minWidth = 0.0 - selectionModel.selectLast() + selectionModel.select(binning as Int?) binningProperty.bind(this.selectionModel.selectedItemProperty()) } val normalizeSwitch: CheckBox = CheckBox("Normalize").apply { @@ -106,7 +107,11 @@ class AmplitudeView : View(title = "Numass amplitude spectrum plot", icon = Imag private fun replotOne(key: String, point: DataController.CachedPoint) { plotJobs[key]?.cancel() + frame.plots.remove(Name.ofSingle(key)) plotJobs[key] = app.context.launch { + withContext(Dispatchers.JavaFx) { + progress.invalidate() + } val valueAxis = if (normalize) { NumassAnalyzer.COUNT_RATE_KEY } else { @@ -134,7 +139,6 @@ class AmplitudeView : View(title = "Numass amplitude spectrum plot", icon = Imag } group } - ensureActive() withContext(Dispatchers.JavaFx) { frame.add(plot) } diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt index 91ab7c99..a7a49e25 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt @@ -40,15 +40,15 @@ class DataController : Controller(), ContextAware { val meta = point.meta - val channelSpectra: Deferred> = context.async { + val channelSpectra: Deferred> = context.async(start = CoroutineStart.LAZY) { point.channels.mapValues { (_, value) -> analyzer.getAmplitudeSpectrum(value) } } - val spectrum: Deferred = context.async { + val spectrum: Deferred
= context.async(start = CoroutineStart.LAZY){ analyzer.getAmplitudeSpectrum(point) } - val timeSpectrum: Deferred
= context.async { + val timeSpectrum: Deferred
= context.async(start = CoroutineStart.LAZY) { val cr = spectrum.await().sumOf { it.getValue(NumassAnalyzer.COUNT_KEY).int }.toDouble() / point.length.toMillis() * 1000 diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/DirectoryWatchView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/DirectoryWatchView.kt index 29372f19..23ee83dd 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/DirectoryWatchView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/DirectoryWatchView.kt @@ -40,7 +40,7 @@ class DirectoryWatchView : View(title = "Numass storage", icon = dfIconView) { val name = Name.of(path.map { it.toString().asName() }) text = null graphic = checkbox(path.fileName.toString()).apply { - isSelected = false + isSelected = dataController.points.containsKey(name) selectedProperty().onChange { if (it) { app.context.launch { diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt index fcd866b6..b5914270 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt @@ -15,10 +15,8 @@ import javafx.geometry.Insets import javafx.geometry.Orientation import javafx.scene.image.ImageView import javafx.util.converter.NumberStringConverter -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.* import kotlinx.coroutines.javafx.JavaFx -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.controlsfx.control.RangeSlider import tornadofx.* import java.util.concurrent.atomic.AtomicInteger @@ -117,18 +115,19 @@ class SpectrumView : View(title = "Numass spectrum plot", icon = ImageView(dfIco val totalProgress = data.values.stream().mapToInt { it.points.size }.sum() data.forEach { (name, set) -> - val plot: DataPlot = - frame.plots[name] as DataPlot? ?: DataPlot(name.toString()).apply { frame.add(this) } + val plot: DataPlot = frame.plots[name] as DataPlot? ?: DataPlot(name.toString()).apply { + frame.add(this) + } app.context.launch { - val points = set.points.map { - dataController.getCachedPoint(Name.join("$name","${it.voltage}[${it.index}]"), it) + val points = set.points.map { point -> + dataController.getCachedPoint(Name.join("$name","${point.voltage}[${point.index}]"), point).also { + it.spectrum.start() + } }.map { cachedPoint -> val count = cachedPoint.spectrum.await().countInWindow(loChannel.toShort(), upChannel.toShort()) val seconds = cachedPoint.length.toMillis() / 1000.0 - launch(Dispatchers.JavaFx) { - container.progress = progress.incrementAndGet().toDouble() / totalProgress - } + Adapters.buildXYDataPoint( cachedPoint.voltage, (count / seconds),