A lot of minor fixes. Moving some code to kotlin

This commit is contained in:
Alexander Nozik 2017-12-15 17:43:44 +03:00
parent 258711c08c
commit 467f0fadfc
11 changed files with 120 additions and 50 deletions

View File

@ -73,8 +73,8 @@ public class NumassDataUtils {
// if (first.getVoltage() != second.getVoltage()) { // if (first.getVoltage() != second.getVoltage()) {
// throw new RuntimeException("Voltage mismatch"); // throw new RuntimeException("Voltage mismatch");
// } // }
// int[] newArray = new int[first.getSpectrum().length]; // int[] newArray = new int[first.getAmplitudeSpectrum().length];
// Arrays.setAll(newArray, i -> first.getSpectrum()[i] + second.getSpectrum()[i]); // Arrays.setAll(newArray, i -> first.getAmplitudeSpectrum()[i] + second.getAmplitudeSpectrum()[i]);
// return new NumassPointImpl( // return new NumassPointImpl(
// first.getVoltage(), // first.getVoltage(),
// Instant.EPOCH, // Instant.EPOCH,
@ -84,8 +84,8 @@ public class NumassDataUtils {
// } // }
// //
// public static NumassPoint substractPoint(NumassPoint point, NumassPoint reference) { // public static NumassPoint substractPoint(NumassPoint point, NumassPoint reference) {
// int[] array = new int[point.getSpectrum().length]; // int[] array = new int[point.getAmplitudeSpectrum().length];
// Arrays.setAll(array, i -> Math.max(0, point.getSpectrum()[i] - reference.getSpectrum()[i])); // Arrays.setAll(array, i -> Math.max(0, point.getAmplitudeSpectrum()[i] - reference.getAmplitudeSpectrum()[i]));
// return new NumassPointImpl( // return new NumassPointImpl(
// point.getVoltage(), // point.getVoltage(),
// point.getTime(), // point.getTime(),

View File

@ -99,9 +99,9 @@ interface NumassAnalyzer {
return analyze(block, config).getValue(LENGTH_KEY).numberValue().toLong() return analyze(block, config).getValue(LENGTH_KEY).numberValue().toLong()
} }
fun getSpectrum(block: NumassBlock, config: Meta): Table { fun getAmplitudeSpectrum(block: NumassBlock, config: Meta): Table {
val seconds = block.length.toMillis().toDouble() / 1000.0 val seconds = block.length.toMillis().toDouble() / 1000.0
return getSpectrum(seconds, getEvents(block, config).asSequence(), config) return getAmplitudeSpectrum(getEvents(block, config).asSequence(), seconds, config)
} }
companion object { companion object {
@ -177,11 +177,12 @@ fun countInWindow(spectrum: Table, loChannel: Short, upChannel: Short): Long {
/** /**
* Calculate the amplitude spectrum for a given block. The s * Calculate the amplitude spectrum for a given block. The s
* *
* @param block * @param events
* @param length length in seconds, used for count rate calculation
* @param config * @param config
* @return * @return
*/ */
fun getSpectrum(length: Double, events: Sequence<NumassEvent>, config: Meta = Meta.empty()): Table { fun getAmplitudeSpectrum(events: Sequence<NumassEvent>, length: Double, config: Meta = Meta.empty()): Table {
val format = TableFormatBuilder() val format = TableFormatBuilder()
.addNumber(NumassAnalyzer.CHANNEL_KEY, X_VALUE_KEY) .addNumber(NumassAnalyzer.CHANNEL_KEY, X_VALUE_KEY)
.addNumber(NumassAnalyzer.COUNT_KEY) .addNumber(NumassAnalyzer.COUNT_KEY)

View File

@ -157,12 +157,12 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
return block.events.count().toDouble() / block.length.toMillis() * 1000 return block.events.count().toDouble() / block.length.toMillis() * 1000
} }
fun getEventsPairs(block: NumassBlock, config: Meta): Sequence<Pair<NumassEvent, NumassEvent>> { fun zipEvents(block: NumassBlock, config: Meta): Sequence<Pair<NumassEvent, NumassEvent>> {
return Sequence { getEvents(block, config).iterator() }.zipWithNext() return getAllEvents(block).asSequence().zipWithNext()
} }
/** /**
* The chain of event times in nanos * The chain of event with delays in nanos
* *
* @param block * @param block
* @param config * @param config
@ -173,21 +173,6 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
val delay = Math.max(next.timeOffset - prev.timeOffset, 0) val delay = Math.max(next.timeOffset - prev.timeOffset, 0)
Pair(prev, delay) Pair(prev, delay)
}.asStream() }.asStream()
// val lastEvent = AtomicReference<NumassEvent>(null)
//
// val eventStream = super.getEvents(block, config)//using super implementation
//
// return eventStream.map { event ->
// var res = if (lastEvent.get() == null) 0L else event.timeOffset - lastEvent.get().timeOffset
//
// if (res < 0) {
// res = 0L
// }
//
// lastEvent.set(event)
// //TODO remove autoboxing somehow
// Pair(event, res)
// }
} }
/** /**

View File

@ -67,8 +67,8 @@ new GrindShell(ctx).eval {
frame.plots.configure(showErrors: false, showSymbol: false, showLine: true, connection: "step") frame.plots.configure(showErrors: false, showSymbol: false, showLine: true, connection: "step")
joined.points.filter { it.voltage in [14000d, 15000d, 16000d, 17000d, 18000d] }.forEach { joined.points.filter { it.voltage in [14000d, 15000d, 16000d, 17000d, 18000d] }.forEach {
//Table spectrum = analyzer.getSpectrum(it, Meta.empty()).withBinning(20).withDeadTime() //Table spectrum = analyzer.getAmplitudeSpectrum(it, Meta.empty()).withBinning(20).withDeadTime()
Table spectrum = analyzer.getSpectrum(it, Grind.buildMeta(t0: t0*1000)).withBinning(20).withDeadTime(t0) Table spectrum = analyzer.getAmplitudeSpectrum(it, Grind.buildMeta(t0: t0*1000)).withBinning(20).withDeadTime(t0)
frame.add(DataPlot.plot(it.voltage.toString(), adapter, spectrum)) frame.add(DataPlot.plot(it.voltage.toString(), adapter, spectrum))
} }
@ -79,7 +79,7 @@ new GrindShell(ctx).eval {
// pointFrame.plots.configure(showErrors: false, showSymbol: false, showLine: true, connection: "step") // pointFrame.plots.configure(showErrors: false, showSymbol: false, showLine: true, connection: "step")
// //
// [0, 5, 10,15,20].forEach{ // [0, 5, 10,15,20].forEach{
// Table spectrum = analyzer.getSpectrum(point, Grind.buildMeta(t0: it*1000)).withBinning(20).withDeadTime(it) // Table spectrum = analyzer.getAmplitudeSpectrum(point, Grind.buildMeta(t0: it*1000)).withBinning(20).withDeadTime(it)
// pointFrame.add(DataPlot.plot(it.toString(), adapter, spectrum)) // pointFrame.add(DataPlot.plot(it.toString(), adapter, spectrum))
// } // }

View File

@ -58,7 +58,7 @@ class UnderflowUtils {
def generate = GrindPipe.build("generate") { def generate = GrindPipe.build("generate") {
result { input -> result { input ->
return analyzer.getSpectrum(input as NumassPoint, delegate.meta) return analyzer.getAmplitudeSpectrum(input as NumassPoint, delegate.meta)
} }
} }

View File

@ -74,7 +74,7 @@ public class UnderflowCorrection {
// } // }
public Values fitPoint(NumassPoint point, int xLow, int xHigh, int upper, int binning) { public Values fitPoint(NumassPoint point, int xLow, int xHigh, int upper, int binning) {
Table spectrum = analyzer.getSpectrum(point, Meta.empty()); Table spectrum = analyzer.getAmplitudeSpectrum(point, Meta.empty());
double norm = spectrum.getRows().filter(row -> { double norm = spectrum.getRows().filter(row -> {
int channel = row.getInt(CHANNEL_KEY); int channel = row.getInt(CHANNEL_KEY);

View File

@ -0,0 +1,73 @@
/*
* Copyright 2017 Alexander Nozik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package inr.numass.scripts
import hep.dataforge.kodex.buildContext
import hep.dataforge.kodex.buildMeta
import inr.numass.NumassPlugin
import inr.numass.data.NumassDataUtils
import inr.numass.data.analyzers.SmartAnalyzer
import inr.numass.data.api.NumassEvent
import inr.numass.data.api.NumassSet
import inr.numass.data.storage.NumassStorageFactory
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation
import java.util.stream.Stream
private fun correlation(sequence: Stream<NumassEvent>): Double {
val amplitudes: MutableList<Double> = ArrayList()
val times: MutableList<Double> = ArrayList()
sequence.forEach {
amplitudes.add(it.chanel.toDouble())
times.add(it.timeOffset.toDouble())
}
return PearsonsCorrelation().correlation(amplitudes.toDoubleArray(), times.toDoubleArray())
}
fun main(args: Array<String>) {
val context = buildContext("NUMASS", NumassPlugin::class.java) {
rootDir = "D:\\Work\\Numass\\sterile\\2017_05"
dataDir = "D:\\Work\\Numass\\data\\2017_05"
}
//val rootDir = File("D:\\Work\\Numass\\data\\2017_05\\Fill_2")
val storage = NumassStorageFactory.buildLocal(context, "Fill_2", true, false);
val sets = (2..14).map { "set_$it" }
val loaders = sets.mapNotNull { set ->
storage.provide("loader::$set", NumassSet::class.java).orElse(null)
}
val set = NumassDataUtils.join("sum", loaders)
val analyzer = SmartAnalyzer();
val meta = buildMeta {
"window.lo" to 400
"window.up" to 2500
}
println("Correlation between amplitudes and delays:")
set.points.filter { it.voltage < 16000.0 }.forEach {
val cor = correlation(analyzer.getEvents(it, meta))
println("${it.voltage}: $cor")
}
}

View File

@ -21,17 +21,21 @@ import hep.dataforge.kodex.buildContext
import hep.dataforge.kodex.buildMeta import hep.dataforge.kodex.buildMeta
import hep.dataforge.kodex.replaceColumn import hep.dataforge.kodex.replaceColumn
import hep.dataforge.plots.data.DataPlot import hep.dataforge.plots.data.DataPlot
import hep.dataforge.tables.Table
import inr.numass.NumassPlugin import inr.numass.NumassPlugin
import inr.numass.data.NumassDataUtils import inr.numass.data.NumassDataUtils
import inr.numass.data.analyzers.NumassAnalyzer import inr.numass.data.analyzers.NumassAnalyzer
import inr.numass.data.analyzers.SmartAnalyzer import inr.numass.data.analyzers.TimeAnalyzer
import inr.numass.data.analyzers.getAmplitudeSpectrum
import inr.numass.data.api.NumassPoint
import inr.numass.data.api.NumassSet import inr.numass.data.api.NumassSet
import inr.numass.data.storage.NumassStorageFactory import inr.numass.data.storage.NumassStorageFactory
fun main(args: Array<String>) { fun main(args: Array<String>) {
val context = buildContext("NUMASS", NumassPlugin::class.java, PlotManager::class.java) { val context = buildContext("NUMASS", NumassPlugin::class.java, PlotManager::class.java) {
rootDir = "D:\\Work\\Numass\\sterile2017_05" rootDir = "D:\\Work\\Numass\\sterile\\2017_05"
dataDir = "D:\\Work\\Numass\\data\\2017_05" dataDir = "D:\\Work\\Numass\\data\\2017_05"
} }
//val rootDir = File("D:\\Work\\Numass\\data\\2017_05\\Fill_2") //val rootDir = File("D:\\Work\\Numass\\data\\2017_05\\Fill_2")
@ -44,14 +48,21 @@ fun main(args: Array<String>) {
storage.provide("loader::$set", NumassSet::class.java).orElse(null) storage.provide("loader::$set", NumassSet::class.java).orElse(null)
} }
val analyzer = SmartAnalyzer()
val all = NumassDataUtils.join("sum", loaders) val all = NumassDataUtils.join("sum", loaders)
val t0 = 20e3
val meta = buildMeta { val meta = buildMeta {
"window.lo" to 400 "window.lo" to 400
"window.up" to 1800 "window.up" to 1800
"t0" to t0
}
val filter: (NumassPoint) -> Table = { point ->
val analyzer = TimeAnalyzer()
val sequence = analyzer.zipEvents(point, meta).filter { it.second.timeOffset - it.first.timeOffset > t0 }.map { it.second }
//val sequence = analyzer.getEvents(point,meta).asSequence()
getAmplitudeSpectrum(sequence, point.length.toMillis().toDouble() / 1000.0, meta)
} }
val plots = context.getFeature(PlotManager::class.java) val plots = context.getFeature(PlotManager::class.java)
@ -60,22 +71,22 @@ fun main(args: Array<String>) {
val integralFrame = plots.getPlotFrame("integral") val integralFrame = plots.getPlotFrame("integral")
for (hv in arrayOf(14000.0, 14200.0, 14400.0, 14600.0, 14800.0, 15000.0)) { for (hv in arrayOf(14000.0, 14500.0, 15000.0, 15500.0)) {
val point1 = all.optPoint(hv).get() val basePoint = all.optPoint(hv).get()
val point0 = all.optPoint(hv + 200.0).get() val subPoint = all.optPoint(hv + 200.0).get()
with(NumassAnalyzer) { with(NumassAnalyzer) {
val spectrum1 = analyzer.getSpectrum(point1, meta).withBinning(20) val baseSpectrum = filter(basePoint).withBinning(50)
val spectrum0 = analyzer.getSpectrum(point0, meta).withBinning(20) val subSpectrum = filter(subPoint).withBinning(50)
val res = subtractAmplitudeSpectrum(spectrum1, spectrum0) val res = subtractAmplitudeSpectrum(baseSpectrum, subSpectrum)
val norm = res.getColumn(COUNT_RATE_KEY).stream().mapToDouble { it.doubleValue() }.sum() val norm = res.getColumn(COUNT_RATE_KEY).stream().mapToDouble { it.doubleValue() }.sum()
integralFrame.add(DataPlot.plot("point_$hv", AMPLITUDE_ADAPTER, spectrum0)) integralFrame.add(DataPlot.plot("point_$hv", AMPLITUDE_ADAPTER, baseSpectrum))
frame.add(DataPlot.plot("point_$hv", AMPLITUDE_ADAPTER, res.replaceColumn(COUNT_RATE_KEY) { getDouble(COUNT_RATE_KEY) / norm })) frame.add(DataPlot.plot("point_$hv", AMPLITUDE_ADAPTER, res.replaceColumn(COUNT_RATE_KEY) { getDouble(COUNT_RATE_KEY) / norm }))
} }

View File

@ -27,7 +27,7 @@ import inr.numass.NumassPlugin
import inr.numass.data.NumassDataUtils import inr.numass.data.NumassDataUtils
import inr.numass.data.analyzers.NumassAnalyzer import inr.numass.data.analyzers.NumassAnalyzer
import inr.numass.data.analyzers.TimeAnalyzer import inr.numass.data.analyzers.TimeAnalyzer
import inr.numass.data.analyzers.getSpectrum import inr.numass.data.analyzers.getAmplitudeSpectrum
import inr.numass.data.api.NumassSet import inr.numass.data.api.NumassSet
import inr.numass.data.storage.NumassStorageFactory import inr.numass.data.storage.NumassStorageFactory
import kotlin.streams.asSequence import kotlin.streams.asSequence
@ -71,16 +71,16 @@ fun main(args: Array<String>) {
} }
with(NumassAnalyzer) { with(NumassAnalyzer) {
val events = getSpectrum(seconds, analyzer.getEvents(point).asSequence(),meta) val events = getAmplitudeSpectrum(analyzer.getEvents(point).asSequence(), seconds, meta)
.withBinning(binning) .withBinning(binning)
val eventsNorming = events.getColumn(COUNT_RATE_KEY).stream().mapToDouble{it.doubleValue()}.sum() val eventsNorming = events.getColumn(COUNT_RATE_KEY).stream().mapToDouble{it.doubleValue()}.sum()
println("The norming factor for unfiltered count rate is $eventsNorming") println("The norming factor for unfiltered count rate is $eventsNorming")
val filtered = getSpectrum( val filtered = getAmplitudeSpectrum(
analyzer.zipEvents(point, Meta.empty()).filter { it.second.timeOffset - it.first.timeOffset > t0 }.map { it.second },
seconds, seconds,
analyzer.getEventsPairs(point, Meta.empty()).filter { it.second.timeOffset - it.first.timeOffset > t0 }.map { it.second },
meta meta
).withBinning(binning) ).withBinning(binning)
@ -88,9 +88,9 @@ fun main(args: Array<String>) {
println("The norming factor for filtered count rate is $filteredNorming") println("The norming factor for filtered count rate is $filteredNorming")
val defaultFiltered = getSpectrum( val defaultFiltered = getAmplitudeSpectrum(
seconds,
analyzer.getEvents(point, buildMeta {"t0" to t0}).asSequence(), analyzer.getEvents(point, buildMeta {"t0" to t0}).asSequence(),
seconds,
meta meta
).withBinning(binning) ).withBinning(binning)

View File

@ -105,7 +105,7 @@ class AmplitudeView(
* Calculate or get spectrum from the cache * Calculate or get spectrum from the cache
*/ */
private suspend fun getSpectrum(point: NumassPoint): Table { private suspend fun getSpectrum(point: NumassPoint): Table {
return cache.computeIfAbsent(point) { analyzer.getSpectrum(point, Meta.empty()) } return cache.computeIfAbsent(point) { analyzer.getAmplitudeSpectrum(point, Meta.empty()) }
} }
/** /**

View File

@ -154,7 +154,7 @@ class SpectrumView(
} }
private fun getSpectrum(point: NumassPoint): Table { private fun getSpectrum(point: NumassPoint): Table {
return cache.computeIfAbsent(point) { analyzer.getSpectrum(point, Meta.empty()) } return cache.computeIfAbsent(point) { analyzer.getAmplitudeSpectrum(point, Meta.empty()) }
} }