diff --git a/build.gradle.kts b/build.gradle.kts index c38b563..95827ee 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ allprojects { val dataforgeVersion by extra("0.5.2") val tablesVersion: String by extra("0.1.5") -val kmathVersion by extra("0.3.0-dev-18") +val kmathVersion by extra("0.3.0") val plotlyVersion: String by extra("0.5.0") ksciencePublish{ diff --git a/numass-analysis/src/jvmMain/kotlin/ru/inr/mass/data/analysis/timeHistogram.kt b/numass-analysis/src/jvmMain/kotlin/ru/inr/mass/data/analysis/timeHistogram.kt index 2a6aa79..44c25e9 100644 --- a/numass-analysis/src/jvmMain/kotlin/ru/inr/mass/data/analysis/timeHistogram.kt +++ b/numass-analysis/src/jvmMain/kotlin/ru/inr/mass/data/analysis/timeHistogram.kt @@ -1,12 +1,14 @@ package ru.inr.mass.data.analysis import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.transform import kotlinx.coroutines.runBlocking import ru.inr.mass.data.api.NumassBlock import ru.inr.mass.data.api.getTime -import space.kscience.kmath.histogram.UnivariateHistogram +import space.kscience.kmath.histogram.Histogram +import space.kscience.kmath.histogram.UniformHistogram1D +import space.kscience.kmath.histogram.uniform1D +import space.kscience.kmath.operations.DoubleField import kotlin.math.max import kotlin.time.DurationUnit @@ -23,7 +25,7 @@ public fun Flow.zipWithNext(block: (l: T, r: T) -> R): Flow { public fun NumassBlock.timeHistogram( binSize: Double, extractor: NumassEventExtractor = NumassEventExtractor.EVENTS_ONLY, -): UnivariateHistogram = UnivariateHistogram.uniform(binSize) { +): UniformHistogram1D = Histogram.uniform1D(DoubleField, binSize).produce { runBlocking { extractor.extract(this@timeHistogram).zipWithNext { l, r -> if(l.owner == r.owner) { diff --git a/numass-model/src/commonMain/kotlin/ru/inr/mass/models/NumassTransmission.kt b/numass-model/src/commonMain/kotlin/ru/inr/mass/models/NumassTransmission.kt index a9ce83f..438865f 100644 --- a/numass-model/src/commonMain/kotlin/ru/inr/mass/models/NumassTransmission.kt +++ b/numass-model/src/commonMain/kotlin/ru/inr/mass/models/NumassTransmission.kt @@ -7,8 +7,8 @@ package ru.inr.mass.models import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.expressions.symbol +import space.kscience.kmath.functions.Function1D import space.kscience.kmath.functions.PiecewisePolynomial -import space.kscience.kmath.functions.UnivariateFunction import space.kscience.kmath.functions.asFunction import space.kscience.kmath.integration.* import space.kscience.kmath.operations.DoubleField @@ -84,7 +84,7 @@ public class NumassTransmission( public val trap: Symbol by symbol public val thickness: Symbol by symbol - private val cache = HashMap>() + private val cache = HashMap>() private const val ION_POTENTIAL = 15.4//eV @@ -130,7 +130,7 @@ public class NumassTransmission( return exp(-x) } - private fun getCachedSpectrum(order: Int): UnivariateFunction { + private fun getCachedSpectrum(order: Int): Function1D { return when { order <= 0 -> error("Non-positive loss cache order") order == 1 -> singleScatterFunction @@ -147,7 +147,7 @@ public class NumassTransmission( * @param order * @return */ - private fun getLoss(order: Int): UnivariateFunction = getCachedSpectrum(order) + private fun getLoss(order: Int): Function1D = getCachedSpectrum(order) private fun getLossProbDerivs(x: Double): List { val res = ArrayList() @@ -260,7 +260,7 @@ public class NumassTransmission( * @return */ @Synchronized - private fun getNextLoss(margin: Double, loss: UnivariateFunction): PiecewisePolynomial { + private fun getNextLoss(margin: Double, loss: Function1D): PiecewisePolynomial { val res = { x: Double -> DoubleField.simpsonIntegrator.integrate(5.0..margin, IntegrandMaxCalls(200)) { y -> loss(x - y) * singleScatterFunction(y) @@ -324,7 +324,7 @@ public class NumassTransmission( private val w1 = 1.85 private val w2 = 12.5 - public val singleScatterFunction: UnivariateFunction = { eps: Double -> + public val singleScatterFunction: Function1D = { eps: Double -> when { eps <= 0 -> 0.0 eps <= b -> { diff --git a/numass-model/src/commonMain/kotlin/ru/inr/mass/models/functionCaching.kt b/numass-model/src/commonMain/kotlin/ru/inr/mass/models/functionCaching.kt index b71eb34..0e08af7 100644 --- a/numass-model/src/commonMain/kotlin/ru/inr/mass/models/functionCaching.kt +++ b/numass-model/src/commonMain/kotlin/ru/inr/mass/models/functionCaching.kt @@ -1,15 +1,15 @@ package ru.inr.mass.models +import space.kscience.kmath.functions.Function1D import space.kscience.kmath.functions.PiecewisePolynomial -import space.kscience.kmath.functions.UnivariateFunction import space.kscience.kmath.interpolation.SplineInterpolator import space.kscience.kmath.interpolation.interpolatePolynomials import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.structures.DoubleBuffer import kotlin.math.abs -public fun UnivariateFunction.cache( +public fun Function1D.cache( range: ClosedFloatingPointRange, numCachePoints: Int, ): PiecewisePolynomial { diff --git a/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/extract_data.kt b/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/extract_data.kt index 293ecc4..be91dc7 100644 --- a/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/extract_data.kt +++ b/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/extract_data.kt @@ -1,7 +1,5 @@ package ru.inr.mass.scripts -import kotlinx.datetime.DateTimeUnit -import kotlinx.datetime.plus import ru.inr.mass.data.api.NumassBlock import ru.inr.mass.data.api.channels import ru.inr.mass.workspace.Numass @@ -29,8 +27,9 @@ fun main() { channels.forEach { (key, block) -> targetDir.resolve("channel-$key.csv").write { block.listFrames().forEach { frame -> - val frameTime = pointTime.plus(frame.timeOffset, DateTimeUnit.NANOSECOND) - writeUtf8String("$frameTime,") +// val frameTime = pointTime.plus(frame.timeOffset, DateTimeUnit.NANOSECOND) +// writeUtf8String("$frameTime,") + writeUtf8String("${frame.timeOffset},") val line = frame.signal.joinToString(",", postfix = "\n" ) writeUtf8String(line) } diff --git a/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/gun_sim.kt b/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/gun_sim.kt index c380d89..ea9c656 100644 --- a/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/gun_sim.kt +++ b/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/gun_sim.kt @@ -5,8 +5,8 @@ import space.kscience.kmath.functions.asFunction import space.kscience.kmath.integration.integrate import space.kscience.kmath.integration.splineIntegrator import space.kscience.kmath.integration.value -import space.kscience.kmath.interpolation.SplineInterpolator import space.kscience.kmath.interpolation.interpolatePolynomials +import space.kscience.kmath.interpolation.splineInterpolator import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.real.step import space.kscience.plotly.Plotly @@ -28,7 +28,7 @@ fun main() { Pair(e.toDouble(), p.toDouble()) }.toList() - val interpolated = SplineInterpolator.double + val interpolated = DoubleField.splineInterpolator .interpolatePolynomials(backScatteringSpectrum) .asFunction(DoubleField, 0.0) diff --git a/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/plots.kt b/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/plots.kt index 01d2699..8372ffb 100644 --- a/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/plots.kt +++ b/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/plots.kt @@ -12,8 +12,8 @@ import ru.inr.mass.data.proto.HVData import ru.inr.mass.data.proto.NumassDirectorySet import space.kscience.dataforge.values.asValue import space.kscience.dataforge.values.double -import space.kscience.kmath.histogram.UnivariateHistogram -import space.kscience.kmath.histogram.center +import space.kscience.kmath.domains.center +import space.kscience.kmath.histogram.Histogram1D import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.asIterable import space.kscience.kmath.structures.Buffer @@ -26,9 +26,9 @@ import kotlin.time.DurationUnit * Plot a kmath histogram */ @OptIn(UnstableKMathAPI::class) -fun Plot.histogram(histogram: UnivariateHistogram, block: Scatter.() -> Unit = {}): Trace = scatter { +fun Plot.histogram(histogram: Histogram1D, block: Scatter.() -> Unit = {}): Trace = scatter { x.numbers = histogram.bins.map { it.domain.center } - y.numbers = histogram.bins.map { it.value } + y.numbers = histogram.bins.map { it.binValue } line.shape = LineShape.hv block() } @@ -58,7 +58,7 @@ fun Plotly.plotNumassBlock( block: NumassBlock, amplitudeBinSize: UInt = 20U, eventExtractor: NumassEventExtractor = NumassEventExtractor.EVENTS_ONLY, - splitChannels: Boolean = true + splitChannels: Boolean = true, ): PlotlyFragment = Plotly.fragment { plot { runBlocking { @@ -105,11 +105,11 @@ fun Plotly.plotNumassSet( h2 { +"Time spectra" } plot { - spectra.forEach { (point,spectrum) -> + spectra.forEach { (point, spectrum) -> val countRate = runBlocking { spectrum.sum().toDouble() / point.getLength().toDouble(DurationUnit.SECONDS) } - val binSize = 1.0 / countRate / 10.0 + val binSize = 1.0 / countRate / 10.0 histogram(point.timeHistogram(binSize)) { name = point.title }