From 65c83f35249986ecb31cff8cc1c0a3f9e7a73038 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 29 May 2021 18:08:43 +0300 Subject: [PATCH] Update coroutines version --- dataforge-core/build.gradle | 2 +- dataforge-gui/build.gradle | 4 ++-- .../kotlin/hep/dataforge/maths/chain/Chain.kt | 15 ++++-------- .../kotlin/hep/dataforge/stat/Sampling.kt | 23 +++++++++++-------- .../numass/scripts/models/IntegralSpectrum.kt | 16 +++++++------ 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/dataforge-core/build.gradle b/dataforge-core/build.gradle index cc6b8308..85abc9fb 100644 --- a/dataforge-core/build.gradle +++ b/dataforge-core/build.gradle @@ -2,7 +2,7 @@ description = 'dataforge-core' dependencies { compile 'ch.qos.logback:logback-classic:1.2.3' - compile 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.3.2' + compile 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.5.0' compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: kotlin_version compile group: 'javax.cache', name: 'cache-api', version: '1.1.0' } diff --git a/dataforge-gui/build.gradle b/dataforge-gui/build.gradle index 98d5657c..fe5e3a92 100644 --- a/dataforge-gui/build.gradle +++ b/dataforge-gui/build.gradle @@ -11,10 +11,10 @@ dependencies { compile project(':dataforge-plots') compile project(':dataforge-gui:dataforge-html') compile 'org.controlsfx:controlsfx:8.40.14' - compile "no.tornado:tornadofx:1.7.17" + compile "no.tornado:tornadofx:1.7.19" compile 'no.tornado:tornadofx-controlsfx:0.1.1' compile group: 'org.fxmisc.richtext', name: 'richtextfx', version: '0.10.2' - compile 'org.jetbrains.kotlinx:kotlinx-coroutines-javafx:1.0.1' + compile 'org.jetbrains.kotlinx:kotlinx-coroutines-javafx:1.5.0' // optional dependency for JFreeChart //compileOnly project(":dataforge-plots:plots-jfc") diff --git a/dataforge-maths/src/main/kotlin/hep/dataforge/maths/chain/Chain.kt b/dataforge-maths/src/main/kotlin/hep/dataforge/maths/chain/Chain.kt index 9bac0d11..5548880e 100644 --- a/dataforge-maths/src/main/kotlin/hep/dataforge/maths/chain/Chain.kt +++ b/dataforge-maths/src/main/kotlin/hep/dataforge/maths/chain/Chain.kt @@ -16,10 +16,9 @@ package hep.dataforge.maths.chain -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.channels.ReceiveChannel -import kotlinx.coroutines.channels.map -import kotlinx.coroutines.channels.produce +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -48,8 +47,7 @@ interface Chain : Sequence { /** * Chain as a coroutine receive channel */ - val channel: ReceiveChannel - get() = GlobalScope.produce { while (true) send(next()) } + val flow: Flow get() = flow { while (true) emit(next()) } override fun iterator(): Iterator { return object : Iterator { @@ -77,10 +75,7 @@ interface Chain : Sequence { return parent.fork().map(func) } - override val channel: ReceiveChannel - get() { - return parent.channel.map { func.invoke(it) } - } + override val flow: Flow get() = parent.flow.map { func.invoke(it) } } } } diff --git a/dataforge-stat/src/main/kotlin/hep/dataforge/stat/Sampling.kt b/dataforge-stat/src/main/kotlin/hep/dataforge/stat/Sampling.kt index 9bb84660..53d35309 100644 --- a/dataforge-stat/src/main/kotlin/hep/dataforge/stat/Sampling.kt +++ b/dataforge-stat/src/main/kotlin/hep/dataforge/stat/Sampling.kt @@ -18,7 +18,8 @@ package hep.dataforge.stat import hep.dataforge.maths.chain.Chain import hep.dataforge.maths.chain.SimpleChain -import kotlinx.coroutines.channels.dropWhile +import kotlinx.coroutines.flow.dropWhile +import kotlinx.coroutines.flow.first import org.apache.commons.math3.distribution.MultivariateRealDistribution import org.apache.commons.math3.distribution.RealDistribution import org.apache.commons.math3.random.RandomGenerator @@ -40,14 +41,15 @@ val MultivariateRealDistribution.chain: Chain * @param targetDensity target probability density */ fun rejectingChain( - proposal: Chain, - proposalDensity: (T) -> Double, - factor: Double = 1.0, - generator: RandomGenerator = defaultGenerator, - targetDensity: (T) -> Double): Chain { + proposal: Chain, + proposalDensity: (T) -> Double, + factor: Double = 1.0, + generator: RandomGenerator = defaultGenerator, + targetDensity: (T) -> Double, +): Chain { return SimpleChain { //TODO check if target density higher than proposal density? - proposal.channel.dropWhile { generator.nextDouble() < targetDensity(it) / proposalDensity(it) / factor }.receive() + proposal.flow.dropWhile { generator.nextDouble() < targetDensity(it) / proposalDensity(it) / factor }.first() } } @@ -55,8 +57,9 @@ fun rejectingChain( * Sample given distribution using this disribution and accept-reject method */ fun RealDistribution.rejectingChain( - factor: Double = 1.0, - generator: RandomGenerator = defaultGenerator, - targetDensity: (Double) -> Double): Chain { + factor: Double = 1.0, + generator: RandomGenerator = defaultGenerator, + targetDensity: (Double) -> Double, +): Chain { return rejectingChain(this.chain, this::density, factor, generator, targetDensity) } \ No newline at end of file diff --git a/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt b/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt index 1b3c5ff7..3df13119 100644 --- a/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt +++ b/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt @@ -18,6 +18,7 @@ package inr.numass.scripts.models import hep.dataforge.buildContext import hep.dataforge.configure +import hep.dataforge.fx.FXPlugin import hep.dataforge.fx.output.FXOutputManager import hep.dataforge.io.output.stream import hep.dataforge.meta.Meta @@ -39,6 +40,7 @@ import inr.numass.data.SpectrumGenerator import inr.numass.models.NBkgSpectrum import inr.numass.models.sterile.SterileNeutrinoSpectrum import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.javafx.JavaFx import kotlinx.coroutines.launch import java.io.PrintWriter import kotlin.math.sqrt @@ -46,7 +48,7 @@ import kotlin.math.sqrt fun main() { - val context = buildContext("NUMASS", NumassPlugin::class.java, JFreeChartPlugin::class.java) { + val context = buildContext("NUMASS", NumassPlugin::class.java, FXPlugin::class.java, JFreeChartPlugin::class.java) { output = FXOutputManager() } @@ -71,9 +73,9 @@ fun main() { } } - (14000.0..18600.0 step 10.0).forEach { - println("$it\t${spectrum.value(it,params)}") - } +// (14000.0..18600.0 step 10.0).forEach { +// println("$it\t${spectrum.value(it,params)}") +// } fun plotSpectrum(name: String, vararg override: Pair): Plot { val x = (14000.0..18600.0).step(100.0).toList() @@ -141,7 +143,7 @@ fun main() { } plots.setType() +plotResidual("trap", "trap" to 0.99) - context.launch(Dispatchers.Main) { + context.launch(Dispatchers.JavaFx) { try { +plotFitResidual("trap_fit", "trap" to 0.99) } catch (ex: Exception) { @@ -149,7 +151,7 @@ fun main() { } } +plotResidual("X", "X" to 0.11) - context.launch(Dispatchers.Main) { + context.launch(Dispatchers.JavaFx) { +plotFitResidual("X_fit", "X" to 0.11) } +plotResidual("sterile_1", "U2" to 1e-3) @@ -157,7 +159,7 @@ fun main() { +plotFitResidual("sterile_1_fit", "U2" to 1e-3) } +plotResidual("sterile_3", "msterile2" to (3000 * 3000).toDouble(), "U2" to 1e-3) - context.launch(Dispatchers.Main) { + context.launch(Dispatchers.JavaFx) { +plotFitResidual("sterile_3_fit", "msterile2" to (3000 * 3000).toDouble(), "U2" to 1e-3) }