This commit is contained in:
Alexander Nozik 2021-01-31 12:08:00 +03:00
parent 7623007bdf
commit 792670c301
6 changed files with 40 additions and 25 deletions

View File

@ -5,20 +5,23 @@ import hep.dataforge.meta.get
import hep.dataforge.meta.string import hep.dataforge.meta.string
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.toInstant 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 data class HVEntry(val timestamp: Instant, val value: Double, val channel: Int = 1) {
public companion object { public companion object {
public fun readString(line: String): HVEntry { public fun readString(line: String): HVEntry {
val (timeStr, channelStr, valueStr) = line.split(' ') 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<HVEntry> { public fun readEnvelope(envelope: Envelope): List<HVEntry> {
check(envelope.meta["type"].string == "voltage"){"Expecting voltage type envelope"} check(envelope.meta["type"].string == "voltage"){"Expecting voltage type envelope"}
return buildList { return buildList {
envelope.data?.read { envelope.data?.read {
forEachUtf8Line { str -> add(readString(str)) } //Some problems with readLines
asInputStream().bufferedReader().lines().forEach { str->
add(readString(str))
}
} }
} }
} }

View File

@ -45,7 +45,7 @@ public class NumassDirectorySet internal constructor(
} }
@OptIn(DFExperimental::class) @OptIn(DFExperimental::class)
public val hvData: List<HVEntry>? get(){ public fun getHvData(): List<HVEntry>? {
val hvFile = path / "voltage" val hvFile = path / "voltage"
return if (hvFile.exists()) { return if (hvFile.exists()) {
val envelope = context.io.readEnvelopeFile(hvFile) val envelope = context.io.readEnvelopeFile(hvFile)

View File

@ -11,7 +11,8 @@
"*mavenLocal", "*mavenLocal",
"https://dl.bintray.com/mipt-npm/dataforge", "https://dl.bintray.com/mipt-npm/dataforge",
"https://dl.bintray.com/mipt-npm/kscience", "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": { "properties": {
"v": "0.1.0-SNAPSHOT" "v": "0.1.0-SNAPSHOT"
@ -29,6 +30,6 @@
"kscience.plotly.Plot": "HTML(JupyterPlotly.renderPlot($it))", "kscience.plotly.Plot": "HTML(JupyterPlotly.renderPlot($it))",
"kscience.plotly.PlotlyFragment": "HTML(JupyterPlotly.renderFragment($it))", "kscience.plotly.PlotlyFragment": "HTML(JupyterPlotly.renderFragment($it))",
"kscience.plotly.PlotlyPage": "HTML(JupyterPlotly.renderPage($it), true)", "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)"
} }
} }

View File

@ -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()
}

View File

@ -14,7 +14,7 @@ import ru.inr.mass.data.api.NumassPoint
fun NumassPoint.spectrum(): UnivariateHistogram = fun NumassPoint.spectrum(): UnivariateHistogram =
UnivariateHistogram.uniform(1.0) { UnivariateHistogram.uniform(1.0) {
runBlocking { runBlocking {
events.collect { put(it.channel.toDouble()) } events.collect { put(it.amplitude.toDouble()) }
} }
} }
@ -26,7 +26,7 @@ fun Collection<NumassPoint>.spectrum(): UnivariateHistogram {
return UnivariateHistogram.uniform(1.0) { return UnivariateHistogram.uniform(1.0) {
runBlocking { runBlocking {
this@spectrum.forEach { point -> 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()) { ): UnivariateHistogram = UnivariateHistogram.uniform(binSize.toDouble()) {
this@reShape.filter { it.position.toInt() in channelRange }.forEach { bin -> this@reShape.filter { it.position.toInt() in channelRange }.forEach { bin ->
if(bin.size > binSize.toDouble()) error("Can't reShape the spectrum with increased binning") 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())
} }
} }

View File

@ -5,8 +5,6 @@ import kotlinx.html.h2
import kscience.kmath.histogram.UnivariateHistogram import kscience.kmath.histogram.UnivariateHistogram
import kscience.kmath.misc.UnstableKMathAPI import kscience.kmath.misc.UnstableKMathAPI
import kscience.plotly.* import kscience.plotly.*
import kscience.plotly.models.Bar
import kscience.plotly.models.Scatter
import kscience.plotly.models.Trace import kscience.plotly.models.Trace
import ru.inr.mass.data.api.NumassPoint import ru.inr.mass.data.api.NumassPoint
import ru.inr.mass.data.proto.HVEntry import ru.inr.mass.data.proto.HVEntry
@ -18,28 +16,30 @@ fun Trace.fromSpectrum(histogram: UnivariateHistogram) {
y.numbers = histogram.map { it.value } y.numbers = histogram.map { it.value }
} }
/**
*
*/
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
fun Plot.spectrum(name: String, histogram: UnivariateHistogram): Bar = bar { fun Plot.spectrum(name: String, histogram: UnivariateHistogram): Trace = scatter {
this.name = name this.name = name
fromSpectrum(histogram) 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)) spectrum(name, point.spectrum().reShape(binSize, range))
} }
/** /**
* Generate a plot from hv data * Generate a plot from hv data
*/ */
fun Plot.hvData(data: List<HVEntry>): Scatter = scatter { fun Plot.hvData(data: List<HVEntry>): Trace = scatter {
x.strings = data.map { it.timestamp.toString() } x.strings = data.map { it.timestamp.toString() }
y.numbers = data.map { it.value } 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 { h1 {
+"Numass point set $path" +"Numass point set $path"
} }
@ -47,11 +47,11 @@ fun NumassDirectorySet.plotlyPage(binSize: Int = 20, range: IntRange = 0..4096):
+"Amplitude spectrum" +"Amplitude spectrum"
} }
plot { plot {
points.forEach { points.sortedBy { it.index }.forEach {
amplitudeSpectrum(it, binSize, range) amplitudeSpectrum(it, binSize, range)
} }
} }
hvData?.let {entries-> getHvData()?.let { entries ->
h2 { h2 {
+"HV" +"HV"
} }