Updates in numass viewer. Minor fixes in core.

This commit is contained in:
Alexander Nozik 2016-05-22 22:58:01 +03:00
parent d65c29cde8
commit 3767d13756
8 changed files with 156 additions and 138 deletions

View File

@ -1,87 +0,0 @@
/*
* Copyright 2015 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.actions;
import hep.dataforge.tables.MapPoint;
import hep.dataforge.tables.SimplePointSource;
import hep.dataforge.values.Value;
import inr.numass.storage.NMFile;
import inr.numass.storage.NMPoint;
import inr.numass.storage.NumassData;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Darksnake
*/
public class BorderData extends SimplePointSource {
private final static String[] names = {"U", "80%", "90%", "95%", "99%"};
private final static double[] percents = {0.8, 0.9, 0.95, 0.99};
public static double getNorm(Map<Double, Double> spectrum, int lower, int upper) {
double res = 0;
for (Map.Entry<Double, Double> entry : spectrum.entrySet()) {
if ((entry.getKey() >= lower) && (entry.getKey() <= upper)) {
res += entry.getValue();
}
}
return res;
}
public BorderData(NumassData file, int upper, int lower, NMPoint reference) {
super(names);
if (upper <= lower) {
throw new IllegalArgumentException();
}
fill(file, lower, upper, reference);
}
private void fill(NumassData file, int lower, int upper, NMPoint reference) {
for (NMPoint point : file.getNMPoints()) {
if ((reference != null) && (point.getUset() == reference.getUset())) {
continue;
}
//создаем основу для будущей точки
HashMap<String, Value> map = new HashMap<>();
map.put(names[0], Value.of(point.getUset()));
Map<Double, Double> spectrum;
if (reference != null) {
spectrum = point.getMapWithBinning(reference, 0);
} else {
spectrum = point.getMapWithBinning(0, true);
}
double norm = getNorm(spectrum, lower, upper);
double counter = 0;
int chanel = upper;
while (chanel > lower) {
chanel--;
counter += spectrum.get((double) chanel);
for (int i = 0; i < percents.length; i++) {
if (counter / norm > percents[i]) {
if (!map.containsKey(names[i + 1])) {
map.put(names[i + 1], Value.of(chanel));
}
}
}
}
this.addRow(new MapPoint(map));
}
}
}

View File

@ -22,20 +22,29 @@ import hep.dataforge.exceptions.ContentException;
import hep.dataforge.io.ColumnedDataWriter; import hep.dataforge.io.ColumnedDataWriter;
import hep.dataforge.io.reports.Reportable; import hep.dataforge.io.reports.Reportable;
import hep.dataforge.meta.Laminate; import hep.dataforge.meta.Laminate;
import hep.dataforge.tables.ListTable;
import hep.dataforge.tables.MapPoint;
import hep.dataforge.tables.Table;
import hep.dataforge.values.Value;
import inr.numass.storage.NMFile; import inr.numass.storage.NMFile;
import inr.numass.storage.NMPoint; import inr.numass.storage.NMPoint;
import inr.numass.storage.NumassData; import inr.numass.storage.NumassData;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/** /**
* *
* @author Darksnake * @author Darksnake
*/ */
@TypedActionDef(name = "findBorder", inputType = NMFile.class, outputType = NMFile.class) @TypedActionDef(name = "findBorder", inputType = NMFile.class, outputType = Table.class)
public class FindBorderAction extends OneToOneAction<NumassData, NumassData> { public class FindBorderAction extends OneToOneAction<NumassData, Table> {
private final static String[] names = {"U", "80%", "90%", "95%", "99%"};
private final static double[] percents = {0.8, 0.9, 0.95, 0.99};
@Override @Override
protected NumassData execute(Context context, Reportable log, String name, Laminate meta, NumassData source) throws ContentException { protected Table execute(Context context, Reportable log, String name, Laminate meta, NumassData source) throws ContentException {
log.report("File {} started", source.getName()); log.report("File {} started", source.getName());
int upperBorder = meta.getInt("upper", 4094); int upperBorder = meta.getInt("upper", 4094);
@ -50,14 +59,60 @@ public class FindBorderAction extends OneToOneAction<NumassData, NumassData> {
} }
} }
BorderData bData = new BorderData(source, upperBorder, lowerBorder, referencePoint); ListTable.Builder dataBuilder = new ListTable.Builder(names);
fill(dataBuilder, source, lowerBorder, upperBorder, referencePoint);
Table bData = dataBuilder.build();
OutputStream stream = buildActionOutput(context, name); OutputStream stream = buildActionOutput(context, name);
ColumnedDataWriter.writeDataSet(stream, bData, String.format("%s : lower = %d upper = %d", source.getName(), lowerBorder, upperBorder)); ColumnedDataWriter.writeDataSet(stream, bData, String.format("%s : lower = %d upper = %d", name, lowerBorder, upperBorder));
log.report("File {} completed", source.getName()); log.report("File {} completed", source.getName());
return source; return bData;
}
private double getNorm(Map<Double, Double> spectrum, int lower, int upper) {
double res = 0;
for (Map.Entry<Double, Double> entry : spectrum.entrySet()) {
if ((entry.getKey() >= lower) && (entry.getKey() <= upper)) {
res += entry.getValue();
}
}
return res;
}
private void fill(ListTable.Builder dataBuilder, NumassData file, int lower, int upper, NMPoint reference) {
for (NMPoint point : file.getNMPoints()) {
if ((reference != null) && (point.getUset() == reference.getUset())) {
continue;
}
//создаем основу для будущей точки
HashMap<String, Value> map = new HashMap<>();
map.put(names[0], Value.of(point.getUset()));
Map<Double, Double> spectrum;
if (reference != null) {
spectrum = point.getMapWithBinning(reference, 0);
} else {
spectrum = point.getMapWithBinning(0, true);
}
double norm = getNorm(spectrum, lower, upper);
double counter = 0;
int chanel = upper;
while (chanel > lower) {
chanel--;
counter += spectrum.get((double) chanel);
for (int i = 0; i < percents.length; i++) {
if (counter / norm > percents[i]) {
if (!map.containsKey(names[i + 1])) {
map.put(names[i + 1], Value.of(chanel));
}
}
}
}
dataBuilder.addRow(new MapPoint(map));
}
} }
} }

View File

@ -24,6 +24,7 @@ import hep.dataforge.tables.DataPoint;
import hep.dataforge.tables.PointSource; import hep.dataforge.tables.PointSource;
import hep.dataforge.tables.Table; import hep.dataforge.tables.Table;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -39,7 +40,7 @@ public class TransmissionInterpolator implements UnivariateFunction {
public static TransmissionInterpolator fromFile(Context context, String path, String xName, String yName, int nSmooth, double w, double border) { public static TransmissionInterpolator fromFile(Context context, String path, String xName, String yName, int nSmooth, double w, double border) {
try { try {
File dataFile = context.io().getFile(path); File dataFile = context.io().getFile(path);
ColumnedDataReader reader = new ColumnedDataReader(dataFile); ColumnedDataReader reader = new ColumnedDataReader(new FileInputStream(dataFile));
return new TransmissionInterpolator(reader, xName, yName, nSmooth, w, border); return new TransmissionInterpolator(reader, xName, yName, nSmooth, w, border);
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);

View File

@ -7,8 +7,10 @@ package inr.numass.storage;
import hep.dataforge.meta.Meta; import hep.dataforge.meta.Meta;
import hep.dataforge.names.Named; import hep.dataforge.names.Named;
import hep.dataforge.tables.Table;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
/** /**
* *
@ -26,6 +28,10 @@ public interface NumassData extends Named {
Instant startTime(); Instant startTime();
default Supplier<Table> getHVData() {
return () -> null;
}
/** /**
* Find first point with given Uset * Find first point with given Uset
* *

View File

@ -18,6 +18,7 @@ package inr.numass.storage;
import hep.dataforge.context.GlobalContext; import hep.dataforge.context.GlobalContext;
import hep.dataforge.data.binary.Binary; import hep.dataforge.data.binary.Binary;
import hep.dataforge.exceptions.StorageException; import hep.dataforge.exceptions.StorageException;
import hep.dataforge.io.ColumnedDataReader;
import hep.dataforge.io.envelopes.DefaultEnvelopeReader; import hep.dataforge.io.envelopes.DefaultEnvelopeReader;
import hep.dataforge.io.envelopes.Envelope; import hep.dataforge.io.envelopes.Envelope;
import hep.dataforge.meta.Meta; import hep.dataforge.meta.Meta;
@ -25,7 +26,10 @@ import hep.dataforge.meta.MetaBuilder;
import hep.dataforge.storage.api.ObjectLoader; import hep.dataforge.storage.api.ObjectLoader;
import hep.dataforge.storage.api.Storage; import hep.dataforge.storage.api.Storage;
import hep.dataforge.storage.loaders.AbstractLoader; import hep.dataforge.storage.loaders.AbstractLoader;
import hep.dataforge.tables.ListTable;
import hep.dataforge.tables.Table;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
@ -40,6 +44,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.vfs2.FileObject; import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException; import org.apache.commons.vfs2.FileSystemException;
@ -263,6 +269,30 @@ public class NumassDataLoader extends AbstractLoader implements ObjectLoader<Env
.meta(); .meta();
} }
@Override
public Supplier<Table> getHVData() {
Envelope hvEnvelope = getHVEnvelope();
if (hvEnvelope == null) {
return () -> null;
}
return () -> {
try {
return new ColumnedDataReader(hvEnvelope.getData().getStream(), "timestamp", "block", "value").toDataSet();
} catch (IOException ex) {
LoggerFactory.getLogger(getClass()).error("Failed to load HV data from file", ex);
return null;
}
};
}
private Envelope getHVEnvelope() {
if (getItems().containsKey(HV_FRAGMENT_NAME)) {
return getItems().get(HV_FRAGMENT_NAME).get();
} else {
return null;
}
}
// public Envelope getHvData() { // public Envelope getHvData() {
// return hvData; // return hvData;
// } // }

View File

@ -5,7 +5,7 @@ if (!hasProperty('mainClass')) {
} }
mainClassName = mainClass mainClassName = mainClass
version = "0.3.2" version = "0.3.3"
description = "The viewer for numass data" description = "The viewer for numass data"

View File

@ -28,8 +28,11 @@ import hep.dataforge.meta.MetaBuilder;
import hep.dataforge.plots.XYPlotFrame; import hep.dataforge.plots.XYPlotFrame;
import hep.dataforge.plots.XYPlottable; import hep.dataforge.plots.XYPlottable;
import hep.dataforge.plots.data.ChangeablePlottableData; import hep.dataforge.plots.data.ChangeablePlottableData;
import hep.dataforge.plots.data.DynamicPlottable;
import hep.dataforge.plots.data.DynamicPlottableSet;
import hep.dataforge.plots.data.PlotDataUtils; import hep.dataforge.plots.data.PlotDataUtils;
import hep.dataforge.plots.data.PlottableData; import hep.dataforge.plots.data.PlottableData;
import hep.dataforge.plots.data.PlottableSet;
import hep.dataforge.plots.fx.PlotContainer; import hep.dataforge.plots.fx.PlotContainer;
import hep.dataforge.plots.jfreechart.JFreeChartFrame; import hep.dataforge.plots.jfreechart.JFreeChartFrame;
import hep.dataforge.storage.commons.JSONMetaWriter; import hep.dataforge.storage.commons.JSONMetaWriter;
@ -47,12 +50,12 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.concurrent.Task;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@ -90,7 +93,8 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
Logger logger = LoggerFactory.getLogger(NumassLoaderViewComponent.class); Logger logger = LoggerFactory.getLogger(NumassLoaderViewComponent.class);
private NumassData data; private NumassData data;
private PlotContainer detectorPlot; private PlotContainer detectorPlot;
private XYPlotFrame spectrumPlotFrame; private PlotContainer spectrumPlot;
private PlotContainer hvPlot;
private ChangeablePlottableData spectrumData; private ChangeablePlottableData spectrumData;
private List<NMPoint> points; private List<NMPoint> points;
private ChoiceBox<Integer> detectorBinningSelector; private ChoiceBox<Integer> detectorBinningSelector;
@ -123,6 +127,8 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
private Button spectrumExportButton; private Button spectrumExportButton;
@FXML @FXML
private TextField dTimeField; private TextField dTimeField;
@FXML
private AnchorPane hvPane;
public NumassLoaderViewComponent(Context context) { public NumassLoaderViewComponent(Context context) {
this.context = context; this.context = context;
@ -166,8 +172,17 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
detectorPlot.addToSideBar(detectorDataExportButton); detectorPlot.addToSideBar(detectorDataExportButton);
detectorPlot.setSideBarPosition(0.7); detectorPlot.setSideBarPosition(0.7);
//setup spectrum pane //setup spectrum pane
spectrumPlot = PlotContainer.anchorTo(spectrumPlotPane);
Meta spectrumPlotMeta = new MetaBuilder("plot")
.setValue("xAxis.axisTitle", "U")
.setValue("xAxis.axisUnits", "V")
.setValue("yAxis.axisTitle", "count rate")
.setValue("yAxis.axisUnits", "Hz")
.setValue("legend.show", false);
spectrumPlot.setPlot(new JFreeChartFrame(spectrumPlotMeta));
lowChannelField.textProperty().bindBidirectional(channelSlider.lowValueProperty(), new NumberStringConverter()); lowChannelField.textProperty().bindBidirectional(channelSlider.lowValueProperty(), new NumberStringConverter());
upChannelField.textProperty().bindBidirectional(channelSlider.highValueProperty(), new NumberStringConverter()); upChannelField.textProperty().bindBidirectional(channelSlider.highValueProperty(), new NumberStringConverter());
@ -175,11 +190,11 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
channelSlider.setLowValue(300d); channelSlider.setLowValue(300d);
ChangeListener<? super Number> rangeChangeListener = (ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> { ChangeListener<? super Number> rangeChangeListener = (ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
updateSpectrumPane(points); setupSpectrumPane(points);
}; };
dTimeField.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> { dTimeField.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
updateSpectrumPane(points); setupSpectrumPane(points);
}); });
channelSlider.lowValueProperty().addListener(rangeChangeListener); channelSlider.lowValueProperty().addListener(rangeChangeListener);
@ -196,6 +211,14 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
}; };
validationSupport.registerValidator(dTimeField, Validator.createPredicateValidator(isNumber, "Must be number")); validationSupport.registerValidator(dTimeField, Validator.createPredicateValidator(isNumber, "Must be number"));
//setup HV plot
hvPlot = PlotContainer.anchorTo(hvPane);
Meta hvPlotMeta = new MetaBuilder("plot")
.setValue("xAxis.axisTitle", "time")
.setValue("xAxis.type", "time")
.setValue("yAxis.axisTitle", "HV");
hvPlot.setPlot(new JFreeChartFrame(hvPlotMeta));
} }
public NumassData getData() { public NumassData getData() {
@ -208,43 +231,45 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
context.processManager().<List<NMPoint>>post("viewer.numass.load", (ProcessManager.Callback callback) -> { context.processManager().<List<NMPoint>>post("viewer.numass.load", (ProcessManager.Callback callback) -> {
callback.updateTitle("Load numass data (" + data.getName() + ")"); callback.updateTitle("Load numass data (" + data.getName() + ")");
points = data.getNMPoints(); points = data.getNMPoints();
Platform.runLater(() -> { Platform.runLater(() -> {
//setup detector data //setup detector data
setupDetectorPane(points); setupDetectorPane(points);
//setup spectrum plot //setup spectrum plot
updateSpectrumPane(points); setupSpectrumPane(points);
setupInfo(data);
}); });
}); });
//setup hv plot
Supplier<Table> hvData = data.getHVData();
if (hvData != null) {
setupHVPane(hvData);
}
setupInfo(data);
} else { } else {
logger.error("The data model is null"); logger.error("The data model is null");
} }
detectorTab.getTabPane().getSelectionModel().select(detectorTab); detectorTab.getTabPane().getSelectionModel().select(detectorTab);
} }
private class LoadPointsTask extends Task<List<NMPoint>> { private void setupHVPane(Supplier<Table> hvData) {
context.processManager().post("viewer.numass.hv", (ProcessManager.Callback callback) -> {
private final NumassData loader; Table t = hvData.get();
public LoadPointsTask(NumassData loader) {
this.loader = loader;
}
@Override
protected List<NMPoint> call() throws Exception {
updateTitle("Load numass data (" + loader.getName() + ")");
List<NMPoint> points = loader.getNMPoints();
Platform.runLater(() -> { Platform.runLater(() -> {
//setup detector data if (t != null) {
setupDetectorPane(points); hvPlot.getPlot().plottables().clear();
//setup spectrum plot DynamicPlottableSet set = new DynamicPlottableSet();
updateSpectrumPane(points); for (DataPoint dp : t) {
String block = dp.getString("block", "default");
setupInfo(data); if (!set.hasPlottable(block)) {
set.addPlottable(new DynamicPlottable(block, block));
}
set.getPlottable(block).put(dp.getValue("timestamp").timeValue(), dp.getValue("value"));
}
hvPlot.getPlot().addAll(set);
}
}); });
return points; });
}
} }
/** /**
@ -274,22 +299,10 @@ public class NumassLoaderViewComponent extends AnchorPane implements Initializab
replace("\\r", "\r\t").replace("\\n", "\n\t")); replace("\\r", "\r\t").replace("\\n", "\n\t"));
} }
private void updateSpectrumPane(List<NMPoint> points) { private void setupSpectrumPane(List<NMPoint> points) {
if (spectrumPlotFrame == null) {
Meta plotMeta = new MetaBuilder("plot")
.setValue("xAxis.axisTitle", "U")
.setValue("xAxis.axisUnits", "V")
.setValue("yAxis.axisTitle", "count rate")
.setValue("yAxis.axisUnits", "Hz")
.setValue("legend.show", false);
spectrumPlotFrame = new JFreeChartFrame(plotMeta).display(spectrumPlotPane);
}
if (spectrumData == null) { if (spectrumData == null) {
spectrumData = new ChangeablePlottableData("spectrum"); spectrumData = new ChangeablePlottableData("spectrum");
spectrumPlotFrame.add(spectrumData); spectrumPlot.getPlot().add(spectrumData);
} }
int lowChannel = (int) channelSlider.getLowValue(); int lowChannel = (int) channelSlider.getLowValue();

View File

@ -52,7 +52,7 @@ limitations under the License.
</Tab> </Tab>
<Tab fx:id="hvTab" text="HV"> <Tab fx:id="hvTab" text="HV">
<content> <content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> <AnchorPane fx:id="hvPane" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content> </content>
</Tab> </Tab>
<Tab fx:id="spectrumTab" text="Spectrum"> <Tab fx:id="spectrumTab" text="Spectrum">