Add HV and plots for set

This commit is contained in:
Alexander Nozik 2021-01-31 11:17:55 +03:00
parent 71e55ed5fc
commit 7623007bdf
7 changed files with 159 additions and 6 deletions

View File

@ -0,0 +1,27 @@
package ru.inr.mass.data.proto
import hep.dataforge.io.Envelope
import hep.dataforge.meta.get
import hep.dataforge.meta.string
import kotlinx.datetime.Instant
import kotlinx.datetime.toInstant
import kotlinx.io.text.forEachUtf8Line
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())
}
public fun readEnvelope(envelope: Envelope): List<HVEntry> {
check(envelope.meta["type"].string == "voltage"){"Expecting voltage type envelope"}
return buildList {
envelope.data?.read {
forEachUtf8Line { str -> add(readString(str)) }
}
}
}
}
}

View File

@ -23,7 +23,7 @@ public class NumassDirectorySet internal constructor(
override val meta: Meta by lazy { override val meta: Meta by lazy {
val metaFilePath = path / "meta" val metaFilePath = path / "meta"
if (metaFilePath.exists()) { if (metaFilePath.exists()) {
val envelope = context.io.readEnvelopeFile(metaFilePath) ?: error("Envelope could not be read from $metaFilePath") val envelope = context.io.readEnvelopeFile(metaFilePath)
envelope.meta envelope.meta
} else { } else {
context.logger.warn { "Meta file does not exist for Numass set $metaFilePath" } context.logger.warn { "Meta file does not exist for Numass set $metaFilePath" }
@ -31,7 +31,7 @@ public class NumassDirectorySet internal constructor(
} }
} }
override val points: List<NumassPoint> by lazy<List<NumassPoint>> { override val points: List<NumassPoint> by lazy {
Files.list(path).filter { Files.list(path).filter {
it.fileName.name.startsWith("p") it.fileName.name.startsWith("p")
}.map { pointPath -> }.map { pointPath ->
@ -43,11 +43,22 @@ public class NumassDirectorySet internal constructor(
} }
}.toList().filterNotNull() }.toList().filterNotNull()
} }
@OptIn(DFExperimental::class)
public val hvData: List<HVEntry>? get(){
val hvFile = path/"voltage"
return if( hvFile.exists()){
val envelope = context.io.readEnvelopeFile(hvFile)
HVEntry.readEnvelope(envelope)
} else {
null
}
}
} }
@OptIn(DFExperimental::class) @OptIn(DFExperimental::class)
public fun Context.readNumassPointFile(path: Path): NumassPoint? { public fun Context.readNumassPointFile(path: Path): NumassPoint? {
val envelope = io.readEnvelopeFile(path) ?: error("Envelope could not be read from $path") val envelope = io.readEnvelopeFile(path)
return ProtoNumassPoint.fromEnvelope(envelope) return ProtoNumassPoint.fromEnvelope(envelope)
} }

View File

@ -0,0 +1,34 @@
{
"imports": [
"kscience.plotly.*",
"kscience.plotly.models.*",
"kscience.plotly.JupyterPlotly",
"hep.dataforge.meta.*",
"kotlinx.html.*",
"ru.inr.mass.workspace.*"
],
"repositories": [
"*mavenLocal",
"https://dl.bintray.com/mipt-npm/dataforge",
"https://dl.bintray.com/mipt-npm/kscience",
"https://dl.bintray.com/mipt-npm/dev"
],
"properties": {
"v": "0.1.0-SNAPSHOT"
},
"link": "https://mipt-npm.jetbrains.space/p/numass/code/numass",
"dependencies": [
"ru.inr.mass:numass-workspace:$v"
],
"init": [
"DISPLAY(HTML(JupyterPlotly.loadJs().toString()))",
"DISPLAY(HTML(\"<p>Plotly.kt jupyter integration is in the development phase. Expect glitches!</p>\"))"
],
"renderers": {
"kscience.plotly.HtmlFragment": "HTML($it.toString())",
"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)"
}
}

View File

@ -20,7 +20,7 @@ fun NumassPoint.spectrum(): UnivariateHistogram =
fun Collection<NumassPoint>.spectrum(): UnivariateHistogram { fun Collection<NumassPoint>.spectrum(): UnivariateHistogram {
if (distinctBy { it.voltage }.size != 1) { if (distinctBy { it.voltage }.size != 1) {
numass.logger.warn { "Spectrum is generated from points with different hv value: $this" } NUMASS.logger.warn { "Spectrum is generated from points with different hv value: $this" }
} }
return UnivariateHistogram.uniform(1.0) { return UnivariateHistogram.uniform(1.0) {
@ -30,4 +30,17 @@ fun Collection<NumassPoint>.spectrum(): UnivariateHistogram {
} }
} }
} }
}
/**
* Re-shape the spectrum with the increased bin size and range. Throws a error if new bin is smaller than before.
*/
fun UnivariateHistogram.reShape(
binSize: Int,
channelRange: IntRange,
): 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())
}
} }

View File

@ -0,0 +1,6 @@
package ru.inr.mass.workspace
import ru.inr.mass.data.proto.NumassDirectorySet
import ru.inr.mass.data.proto.readNumassDirectory
fun readNumassDirectory(path: String): NumassDirectorySet = NUMASS.context.readNumassDirectory(path)

View File

@ -0,0 +1,62 @@
package ru.inr.mass.workspace
import kotlinx.html.h1
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
import ru.inr.mass.data.proto.NumassDirectorySet
@OptIn(UnstableKMathAPI::class)
fun Trace.fromSpectrum(histogram: UnivariateHistogram) {
x.numbers = histogram.map { it.position }
y.numbers = histogram.map { it.value }
}
/**
*
*/
@OptIn(UnstableKMathAPI::class)
fun Plot.spectrum(name: String, histogram: UnivariateHistogram): Bar = bar {
this.name = name
fromSpectrum(histogram)
}
fun Plot.amplitudeSpectrum(point: NumassPoint, binSize: Int = 20, range: IntRange = 0..4096, name: String = point.toString()): Bar = bar {
spectrum(name, point.spectrum().reShape(binSize, range))
}
/**
* Generate a plot from hv data
*/
fun Plot.hvData(data: List<HVEntry>): Scatter = 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 {
h1 {
+"Numass point set $path"
}
h2{
+"Amplitude spectrum"
}
plot {
points.forEach {
amplitudeSpectrum(it,binSize, range)
}
}
hvData?.let {entries->
h2{
+"HV"
}
plot {
hvData(entries)
}
}
}

View File

@ -3,8 +3,8 @@ package ru.inr.mass.workspace
import hep.dataforge.workspace.Workspace import hep.dataforge.workspace.Workspace
import ru.inr.mass.data.proto.NumassProtoPlugin import ru.inr.mass.data.proto.NumassProtoPlugin
val numass = Workspace { val NUMASS = Workspace {
context("numass") { context("NUMASS") {
plugin(NumassProtoPlugin) plugin(NumassProtoPlugin)
} }
} }