Fixed adapters

This commit is contained in:
Alexander Nozik 2017-11-28 21:43:21 +03:00
parent ad96c7a78b
commit 39368b45aa
7 changed files with 57 additions and 6 deletions

View File

@ -1,5 +1,5 @@
plugins { plugins {
id "org.jetbrains.kotlin.jvm" version "1.1.61" apply false id "org.jetbrains.kotlin.jvm" version "1.2.0" apply false
} }
allprojects{ allprojects{

View File

@ -13,6 +13,7 @@ import hep.dataforge.stat.fit.ParamSet
import hep.dataforge.stat.models.XYModel import hep.dataforge.stat.models.XYModel
import hep.dataforge.stat.parametric.ParametricFunction import hep.dataforge.stat.parametric.ParametricFunction
import hep.dataforge.tables.Table import hep.dataforge.tables.Table
import inr.numass.NumassIOKt
import inr.numass.NumassPlugin import inr.numass.NumassPlugin
import inr.numass.data.SpectrumAdapter import inr.numass.data.SpectrumAdapter
import inr.numass.data.SpectrumGenerator import inr.numass.data.SpectrumGenerator
@ -78,4 +79,5 @@ new GrindShell(ctx).eval {
res.printState(ctx.io.out().newPrintWriter()); res.printState(ctx.io.out().newPrintWriter());
NumassIOKt.display(res, ctx, "fit")
} }

View File

@ -21,9 +21,17 @@ import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.Appender import ch.qos.logback.core.Appender
import ch.qos.logback.core.FileAppender import ch.qos.logback.core.FileAppender
import hep.dataforge.context.Context import hep.dataforge.context.Context
import hep.dataforge.fx.plots.plusAssign
import hep.dataforge.io.BasicIOManager import hep.dataforge.io.BasicIOManager
import hep.dataforge.io.IOManager import hep.dataforge.io.IOManager
import hep.dataforge.meta.Meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.plots.PlotUtils
import hep.dataforge.plots.data.DataPlot
import hep.dataforge.plots.data.XYFunctionPlot
import hep.dataforge.stat.fit.FitResult
import hep.dataforge.stat.models.XYModel
import hep.dataforge.tables.Adapters
import hep.dataforge.utils.ReferenceRegistry import hep.dataforge.utils.ReferenceRegistry
import org.apache.commons.io.output.TeeOutputStream import org.apache.commons.io.output.TeeOutputStream
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -134,3 +142,40 @@ class NumassIO : BasicIOManager() {
val NUMASS_OUTPUT_CONTEXT_KEY = "numass.outputDir" val NUMASS_OUTPUT_CONTEXT_KEY = "numass.outputDir"
} }
} }
fun FitResult.display(context: Context, stage: String = "fit") {
val model = optModel(context).get() as XYModel
val adapter = model.adapter
val frame = PlotUtils.getPlotManager(context)
.getPlotFrame(stage, "plot", Meta.empty())
val func = { x: Double -> model.spectrum.value(x, parameters) }
val fit = XYFunctionPlot("fit")
fit.setDensity(100, false)
fit.setSmoothing(true)
fit.setFunction(func)
// ensuring all data points are calculated explicitly
data.rows.map { dp -> Adapters.getXValue(adapter, dp).doubleValue() }.sorted().forEach { fit.calculateIn(it) }
frame.add(fit)
frame.add(DataPlot.plot("data", adapter, data))
val residualsFrame = PlotUtils.getPlotManager(context)
.getPlotFrame(stage, "residuals", Meta.empty())
val residual = DataPlot("residuals");
data.rows.forEach {
val x = Adapters.getXValue(adapter, it).doubleValue()
val y = Adapters.getYValue(adapter, it).doubleValue()
val err = Adapters.optYError(adapter,it).orElse(1.0)
residual += Adapters.buildXYDataPoint(x, (y - func(x)) / err, 1.0)
}
residualsFrame.add(residual)
}

View File

@ -10,6 +10,7 @@ import hep.dataforge.meta.Meta
import hep.dataforge.plots.PlotFrame import hep.dataforge.plots.PlotFrame
import hep.dataforge.plots.data.DataPlot import hep.dataforge.plots.data.DataPlot
import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.plots.jfreechart.JFreeChartFrame
import hep.dataforge.tables.Adapters
import hep.dataforge.tables.Table import hep.dataforge.tables.Table
import inr.numass.data.NumassDataUtils import inr.numass.data.NumassDataUtils
import inr.numass.data.analyzers.SimpleAnalyzer import inr.numass.data.analyzers.SimpleAnalyzer
@ -131,7 +132,7 @@ class AmplitudeView(
} }
DataPlot.plot( DataPlot.plot(
key, key,
XYAdapter(NumassAnalyzer.CHANNEL_KEY, valueAxis), Adapters.buildXYAdapter(NumassAnalyzer.CHANNEL_KEY, valueAxis),
NumassDataUtils.spectrumWithBinning(getSpectrum(point), binning) NumassDataUtils.spectrumWithBinning(getSpectrum(point), binning)
).configure { ).configure {
"connectionType" to "step" "connectionType" to "step"

View File

@ -11,6 +11,7 @@ import hep.dataforge.plots.data.DataPlot
import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.plots.jfreechart.JFreeChartFrame
import hep.dataforge.storage.api.TableLoader import hep.dataforge.storage.api.TableLoader
import hep.dataforge.storage.api.ValueIndex import hep.dataforge.storage.api.ValueIndex
import hep.dataforge.tables.Adapters
import hep.dataforge.tables.ListTable import hep.dataforge.tables.ListTable
import hep.dataforge.tables.Table import hep.dataforge.tables.Table
import hep.dataforge.values.Values import hep.dataforge.values.Values
@ -50,7 +51,7 @@ class SlowControlView : View(title = "Numass slow control view", icon = ImageVie
val group = PlotGroup(change.key) val group = PlotGroup(change.key)
names.forEach { names.forEach {
val adapter = XYAdapter("timestamp", it); val adapter = Adapters.buildXYAdapter("timestamp", it);
val plot = DataPlot.plot(it, adapter, plotData).configure { val plot = DataPlot.plot(it, adapter, plotData).configure {
"showLine" to true "showLine" to true
"showSymbol" to false "showSymbol" to false

View File

@ -3,11 +3,13 @@ package inr.numass.viewer
import hep.dataforge.fx.dfIcon import hep.dataforge.fx.dfIcon
import hep.dataforge.fx.plots.PlotContainer import hep.dataforge.fx.plots.PlotContainer
import hep.dataforge.fx.runGoal import hep.dataforge.fx.runGoal
import hep.dataforge.fx.ui
import hep.dataforge.kodex.configure import hep.dataforge.kodex.configure
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.plots.PlotFrame import hep.dataforge.plots.PlotFrame
import hep.dataforge.plots.data.DataPlot import hep.dataforge.plots.data.DataPlot
import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.plots.jfreechart.JFreeChartFrame
import hep.dataforge.tables.Adapters
import hep.dataforge.tables.Table import hep.dataforge.tables.Table
import inr.numass.data.analyzers.SimpleAnalyzer import inr.numass.data.analyzers.SimpleAnalyzer
import inr.numass.data.api.NumassAnalyzer import inr.numass.data.api.NumassAnalyzer
@ -175,7 +177,7 @@ class SpectrumView(
runLater { runLater {
container.progress = progress.incrementAndGet().toDouble() / totalProgress container.progress = progress.incrementAndGet().toDouble() / totalProgress
} }
XYAdapter.DEFAULT_ADAPTER.buildXYDataPoint( Adapters.buildXYDataPoint(
point.voltage, point.voltage,
(count / seconds), (count / seconds),
Math.sqrt(count.toDouble()) / seconds Math.sqrt(count.toDouble()) / seconds

View File

@ -3,7 +3,7 @@ package inr.numass.viewer.test
import hep.dataforge.fx.plots.PlotContainer import hep.dataforge.fx.plots.PlotContainer
import hep.dataforge.plots.data.DataPlot import hep.dataforge.plots.data.DataPlot
import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.plots.jfreechart.JFreeChartFrame
import hep.dataforge.tables.ValueMap import hep.dataforge.tables.Adapters
import tornadofx.* import tornadofx.*
import java.util.* import java.util.*
@ -20,7 +20,7 @@ class JFCTest : View("My View") {
action { action {
data.fillData( data.fillData(
(1..1000).map { ValueMap.of(arrayOf(XYAdapter.X_VALUE_KEY, XYAdapter.Y_VALUE_KEY), it, rnd.nextDouble()) } (1..1000).map { Adapters.buildXYDataPoint(it.toDouble(), rnd.nextDouble()) }
) )
plot.add(data) plot.add(data)
} }