diff --git a/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/HVEntry.kt b/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/HVEntry.kt index b38fd45..7547b06 100644 --- a/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/HVEntry.kt +++ b/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/HVEntry.kt @@ -5,20 +5,23 @@ import hep.dataforge.meta.get import hep.dataforge.meta.string import kotlinx.datetime.Instant import kotlinx.datetime.toInstant -import kotlinx.io.text.forEachUtf8Line +import kotlinx.io.asInputStream public data class HVEntry(val timestamp: Instant, val value: Double, val channel: Int = 1) { public companion object { public fun readString(line: String): HVEntry { val (timeStr, channelStr, valueStr) = line.split(' ') - return HVEntry(timeStr.toInstant(), valueStr.toDouble(), channelStr.toInt()) + return HVEntry((timeStr+"Z").toInstant(), valueStr.toDouble(), channelStr.toInt()) } public fun readEnvelope(envelope: Envelope): List { check(envelope.meta["type"].string == "voltage"){"Expecting voltage type envelope"} return buildList { envelope.data?.read { - forEachUtf8Line { str -> add(readString(str)) } + //Some problems with readLines + asInputStream().bufferedReader().lines().forEach { str-> + add(readString(str)) + } } } } diff --git a/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/NumassDirectorySet.kt b/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/NumassDirectorySet.kt index 057d100..b6175b8 100644 --- a/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/NumassDirectorySet.kt +++ b/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/NumassDirectorySet.kt @@ -45,9 +45,9 @@ public class NumassDirectorySet internal constructor( } @OptIn(DFExperimental::class) - public val hvData: List? get(){ - val hvFile = path/"voltage" - return if( hvFile.exists()){ + public fun getHvData(): List? { + val hvFile = path / "voltage" + return if (hvFile.exists()) { val envelope = context.io.readEnvelopeFile(hvFile) HVEntry.readEnvelope(envelope) } else { diff --git a/numass-workspace/numass.json b/numass-workspace/numass.json index 39fe1e9..1e21025 100644 --- a/numass-workspace/numass.json +++ b/numass-workspace/numass.json @@ -11,7 +11,8 @@ "*mavenLocal", "https://dl.bintray.com/mipt-npm/dataforge", "https://dl.bintray.com/mipt-npm/kscience", - "https://dl.bintray.com/mipt-npm/dev" + "https://dl.bintray.com/mipt-npm/dev", + "https://kotlin.bintray.com/kotlinx" ], "properties": { "v": "0.1.0-SNAPSHOT" @@ -29,6 +30,6 @@ "kscience.plotly.Plot": "HTML(JupyterPlotly.renderPlot($it))", "kscience.plotly.PlotlyFragment": "HTML(JupyterPlotly.renderFragment($it))", "kscience.plotly.PlotlyPage": "HTML(JupyterPlotly.renderPage($it), true)", - "ru.inr.mass.data.proto.NumassDirectorySet": "HTML(JupyterPlotly.renderPage(${it.plotlyPage}), true)" + "ru.inr.mass.data.proto.NumassDirectorySet": "HTML(JupyterPlotly.renderPage(${it.plotlyPage()}), true)" } } \ No newline at end of file diff --git a/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/demo.kt b/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/demo.kt new file mode 100644 index 0000000..690ebe4 --- /dev/null +++ b/numass-workspace/src/main/kotlin/ru/inr/mass/scripts/demo.kt @@ -0,0 +1,11 @@ +package ru.inr.mass.workspace + +import kscience.plotly.makeFile +import ru.inr.mass.data.proto.readNumassDirectory +import java.nio.file.Path + +fun main() { + val dataPath = Path.of("D:\\Work\\Numass\\data\\2018_04\\Adiabacity_19\\set_4\\") + val testSet = NUMASS.context.readNumassDirectory(dataPath) + testSet.plotlyPage().makeFile() +} \ No newline at end of file diff --git a/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/AmplitudeSpectrum.kt b/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/AmplitudeSpectrum.kt index 2606482..3759310 100644 --- a/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/AmplitudeSpectrum.kt +++ b/numass-workspace/src/main/kotlin/ru/inr/mass/workspace/AmplitudeSpectrum.kt @@ -14,7 +14,7 @@ import ru.inr.mass.data.api.NumassPoint fun NumassPoint.spectrum(): UnivariateHistogram = UnivariateHistogram.uniform(1.0) { runBlocking { - events.collect { put(it.channel.toDouble()) } + events.collect { put(it.amplitude.toDouble()) } } } @@ -26,7 +26,7 @@ fun Collection.spectrum(): UnivariateHistogram { return UnivariateHistogram.uniform(1.0) { runBlocking { this@spectrum.forEach { point -> - point.events.collect { put(it.channel.toDouble()) } + point.events.collect { put(it.amplitude.toDouble()) } } } } @@ -41,6 +41,6 @@ fun UnivariateHistogram.reShape( ): UnivariateHistogram = UnivariateHistogram.uniform(binSize.toDouble()) { this@reShape.filter { it.position.toInt() in channelRange }.forEach { bin -> if(bin.size > binSize.toDouble()) error("Can't reShape the spectrum with increased binning") - putMany(bin.position, bin.value.toLong()) + putMany(bin.position, bin.value.toInt()) } } \ No newline at end of file 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 3ffa2ed..a692cef 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 @@ -5,8 +5,6 @@ import kotlinx.html.h2 import kscience.kmath.histogram.UnivariateHistogram import kscience.kmath.misc.UnstableKMathAPI import kscience.plotly.* -import kscience.plotly.models.Bar -import kscience.plotly.models.Scatter import kscience.plotly.models.Trace import ru.inr.mass.data.api.NumassPoint import ru.inr.mass.data.proto.HVEntry @@ -18,41 +16,43 @@ fun Trace.fromSpectrum(histogram: UnivariateHistogram) { y.numbers = histogram.map { it.value } } -/** - * - */ @OptIn(UnstableKMathAPI::class) -fun Plot.spectrum(name: String, histogram: UnivariateHistogram): Bar = bar { +fun Plot.spectrum(name: String, histogram: UnivariateHistogram): Trace = scatter { this.name = name fromSpectrum(histogram) } -fun Plot.amplitudeSpectrum(point: NumassPoint, binSize: Int = 20, range: IntRange = 0..4096, name: String = point.toString()): Bar = bar { +fun Plot.amplitudeSpectrum( + point: NumassPoint, + binSize: Int = 20, + range: IntRange = 0..2000, + name: String = point.toString(), +): Trace = scatter { spectrum(name, point.spectrum().reShape(binSize, range)) } /** * Generate a plot from hv data */ -fun Plot.hvData(data: List): Scatter = scatter { +fun Plot.hvData(data: List): Trace = scatter { x.strings = data.map { it.timestamp.toString() } y.numbers = data.map { it.value } } -fun NumassDirectorySet.plotlyPage(binSize: Int = 20, range: IntRange = 0..4096): PlotlyPage = Plotly.page { +fun NumassDirectorySet.plotlyPage(binSize: Int = 20, range: IntRange = 0..2000): PlotlyPage = Plotly.page { h1 { +"Numass point set $path" } - h2{ + h2 { +"Amplitude spectrum" } plot { - points.forEach { - amplitudeSpectrum(it,binSize, range) + points.sortedBy { it.index }.forEach { + amplitudeSpectrum(it, binSize, range) } } - hvData?.let {entries-> - h2{ + getHvData()?.let { entries -> + h2 { +"HV" } plot {