Task model helper revision

This commit is contained in:
Alexander Nozik 2017-08-14 22:08:06 +03:00
parent 98cfc04e49
commit 21109003b8
6 changed files with 81 additions and 35 deletions

View File

@ -16,6 +16,9 @@ import static hep.dataforge.io.envelopes.DefaultEnvelopeType.DEFAULT_ENVELOPE_TY
*/
public class NumassEnvelopeType implements EnvelopeType {
public static final byte[] LEGACY_START_SEQUENCE = {'#', '!'};
public static final byte[] LEGACY_END_SEQUENCE = {'!', '#', '\r', '\n'};
@Override
public int getCode() {
return DEFAULT_ENVELOPE_TYPE;
@ -41,18 +44,15 @@ public class NumassEnvelopeType implements EnvelopeType {
return new DefaultEnvelopeWriter(this, MetaType.resolve(properties));
}
private static class LegacyTag extends EnvelopeTag {
private final byte[] START_SEQUENCE = {'#', '!'};
private final byte[] END_SEQUENCE = {'!', '#', '\r', '\n'};
public static class LegacyTag extends EnvelopeTag {
@Override
protected byte[] getStartSequence() {
return START_SEQUENCE;
return LEGACY_START_SEQUENCE;
}
@Override
protected byte[] getEndSequence() {
return END_SEQUENCE;
return LEGACY_END_SEQUENCE;
}
/**

View File

@ -0,0 +1,44 @@
package inr.numass.data.legacy;
import hep.dataforge.io.envelopes.EnvelopeTag;
import hep.dataforge.storage.filestorage.FileEnvelope;
import inr.numass.NumassEnvelopeType;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import static inr.numass.NumassEnvelopeType.LEGACY_START_SEQUENCE;
import static java.nio.file.StandardOpenOption.READ;
public class NumassFileEnvelope extends FileEnvelope {
public static FileEnvelope open(Path path, boolean readOnly) {
if (!Files.exists(path)) {
throw new RuntimeException("File envelope does not exist");
}
try (SeekableByteChannel channel = Files.newByteChannel(path, READ)) {
ByteBuffer header = ByteBuffer.allocate(2);
channel.read(header);
if(Arrays.equals(header.array(),LEGACY_START_SEQUENCE)){
return new NumassFileEnvelope(path,readOnly);
} else {
return FileEnvelope.open(path, readOnly);
}
} catch (IOException e) {
throw new RuntimeException("Failed to open file envelope", e);
}
}
private NumassFileEnvelope(Path path, boolean readOnly) {
super(path, readOnly);
}
@Override
protected EnvelopeTag buildTag(){
return new NumassEnvelopeType.LegacyTag();
}
}

View File

@ -24,13 +24,13 @@ import hep.dataforge.meta.MetaBuilder;
import hep.dataforge.providers.Provider;
import hep.dataforge.storage.api.ObjectLoader;
import hep.dataforge.storage.api.Storage;
import hep.dataforge.storage.filestorage.FileEnvelope;
import hep.dataforge.storage.filestorage.FileStorage;
import hep.dataforge.storage.loaders.AbstractLoader;
import hep.dataforge.tables.Table;
import hep.dataforge.values.Value;
import inr.numass.data.api.NumassPoint;
import inr.numass.data.api.NumassSet;
import inr.numass.data.legacy.NumassFileEnvelope;
import org.slf4j.LoggerFactory;
import java.io.IOException;
@ -87,7 +87,7 @@ public class NumassDataLoader extends AbstractLoader implements ObjectLoader<Env
|| fileName.startsWith(POINT_FRAGMENT_NAME);
}).forEach(file -> {
try {
items.put(FileStorage.entryName(file), () -> FileEnvelope.open(file, true));
items.put(FileStorage.entryName(file), () -> NumassFileEnvelope.open(file, true));
} catch (Exception ex) {
LoggerFactory.getLogger(NumassDataLoader.class)
.error("Can't load numass data directory " + FileStorage.entryName(directory), ex);

View File

@ -27,6 +27,8 @@ import hep.dataforge.tables.Table;
import hep.dataforge.tables.ValueMap;
import hep.dataforge.values.Value;
import hep.dataforge.values.Values;
import inr.numass.data.api.NumassAnalyzer;
import inr.numass.data.api.NumassPoint;
import inr.numass.utils.NumassUtils;
import javafx.util.Pair;
import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
@ -40,6 +42,7 @@ import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;
import static hep.dataforge.values.ValueType.NUMBER;
import static inr.numass.data.analyzers.AbstractAnalyzer.TIME_KEY;
/**
* @author Darksnake
@ -50,7 +53,7 @@ import static hep.dataforge.values.ValueType.NUMBER;
@ValueDef(name = "calculateRelative", info = "Calculate count rate relative to average monitor point", def = "false")
public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
private static final String[] monitorNames = {"Timestamp", "Total", "CR", "CRerr"};
//private static final String[] monitorNames = {"timestamp", NumassAnalyzer.COUNT_KEY, NumassAnalyzer.COUNT_RATE_KEY, NumassAnalyzer.COUNT_RATE_KEY};
CopyOnWriteArrayList<Values> monitorPoints = new CopyOnWriteArrayList<>();
//FIXME remove from state
@ -67,16 +70,16 @@ public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
}
double norm = 0;
double totalAv = 0;
String head = "";
head += String.format("%20s\t%10s\t%s%n", "Timestamp", "Total", "CR in window");
StringBuilder head = new StringBuilder();
head.append(String.format("%20s\t%10s\t%s%n", "timestamp", "Count", "CR in window"));
for (Values dp : index.values()) {
head += String.format("%20s\t%10d\t%g%n", getTime(dp).toString(), getTotal(dp), getCR(dp));
head.append(String.format("%20s\t%10d\t%g%n", getTime(dp).toString(), getTotal(dp), getCR(dp)));
norm += getCR(dp) / index.size();
totalAv += getTotal(dp) / index.size();
monitorPoints.add(dp);
}
head += String.format("%20s\t%10g\t%g%n", "Average", totalAv, norm);
head.append(String.format("%20s\t%10g\t%g%n", "Average", totalAv, norm));
List<Values> dataList = new ArrayList<>();
@ -93,7 +96,7 @@ public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
double corrFactor = corr.getKey();
double corrErr = corr.getValue();
double pointErr = dp.getValue("CRerr").doubleValue() / getCR(dp);
double pointErr = dp.getValue(NumassAnalyzer.COUNT_RATE_ERROR_KEY).doubleValue() / getCR(dp);
double err = Math.sqrt(corrErr * corrErr + pointErr * pointErr) * getCR(dp);
if (dp.getNames().contains("Monitor")) {
@ -102,23 +105,22 @@ public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
pb.putValue("Monitor", corrFactor);
}
pb.putValue("CR", Value.of(dp.getValue("CR").doubleValue() / corrFactor));
pb.putValue("Window", Value.of(dp.getValue("Window").doubleValue() / corrFactor));
pb.putValue("CRerr", Value.of(err));
pb.putValue(NumassAnalyzer.COUNT_RATE_KEY, Value.of(dp.getValue(NumassAnalyzer.COUNT_RATE_KEY).doubleValue() / corrFactor));
pb.putValue(NumassAnalyzer.COUNT_RATE_ERROR_KEY, Value.of(err));
} else {
double corrFactor = dp.getValue("CR").doubleValue() / norm;
double corrFactor = dp.getValue(NumassAnalyzer.COUNT_RATE_KEY).doubleValue() / norm;
if (dp.getNames().contains("Monitor")) {
pb.putValue("Monitor", Value.of(dp.getValue("Monitor").doubleValue() / corrFactor));
} else {
pb.putValue("Monitor", corrFactor);
}
pb.putValue("CR", norm);
pb.putValue(NumassAnalyzer.COUNT_RATE_KEY, norm);
}
if (meta.getBoolean("calculateRelative", false)) {
pb.putValue("relCR", pb.build().getValue("CR").doubleValue() / norm);
pb.putValue("relCRerr", pb.build().getValue("CRerr").doubleValue() / norm);
pb.putValue("relCR", pb.build().getValue(NumassAnalyzer.COUNT_RATE_KEY).doubleValue() / norm);
pb.putValue("relCRerr", pb.build().getValue(NumassAnalyzer.COUNT_RATE_ERROR_KEY).doubleValue() / norm);
}
dataList.add(pb.build());
@ -155,7 +157,7 @@ public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
PolynomialSplineFunction corrFunc = new SplineInterpolator().interpolate(xs, ys);
if (corrFunc.isValidPoint(time)) {
double averageErr = index.values().stream().mapToDouble(p -> p.getDouble("CRerr")).average().getAsDouble();
double averageErr = index.values().stream().mapToDouble(p -> p.getDouble(NumassAnalyzer.COUNT_RATE_ERROR_KEY)).average().getAsDouble();
return new Pair<>(corrFunc.value(time), averageErr / norm / 2d);
} else {
return new Pair<>(1d, 0d);
@ -184,7 +186,7 @@ public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
}
double corrFactor = (getCR(previousMonitor.getValue()) * (1 - p) + getCR(nextMonitor.getValue()) * p) / norm;
double corrErr = previousMonitor.getValue().getValue("CRerr").doubleValue() / getCR(previousMonitor.getValue()) / Math.sqrt(2);
double corrErr = previousMonitor.getValue().getValue(NumassAnalyzer.COUNT_RATE_ERROR_KEY).doubleValue() / getCR(previousMonitor.getValue()) / Math.sqrt(2);
return new Pair<>(corrFactor, corrErr);
}
@ -205,19 +207,19 @@ public class MonitorCorrectAction extends OneToOneAction<Table, Table> {
}
private boolean isMonitorPoint(double monitor, Values point) {
return point.getValue("Uset").doubleValue() == monitor;
return point.getValue(NumassPoint.HV_KEY).doubleValue() == monitor;
}
private Instant getTime(Values point) {
return point.getValue("Timestamp").timeValue();
return point.getValue(TIME_KEY).timeValue();
}
private int getTotal(Values point) {
return point.getValue("Total").intValue();
return point.getValue(NumassAnalyzer.COUNT_KEY).intValue();
}
private double getCR(Values point) {
return point.getValue("CR").doubleValue();
return point.getValue(NumassAnalyzer.COUNT_RATE_KEY).doubleValue();
}
private TreeMap<Instant, Values> getMonitorIndex(double monitor, Iterable<Values> data) {

View File

@ -64,19 +64,19 @@ public class NumassSubstractEmptySourceTask extends AbstractTask<Table> {
return builder.build();
}
@Override
protected TaskModel transformModel(TaskModel model) {
Meta modelMeta = model.meta();
model.dependsOn("prepare", modelMeta, "prepare");
protected void updateModel(TaskModel.Builder model, Meta meta) {
model.dependsOn("prepare", meta, "prepare");
MetaBuilder emptyCfg = new MetaBuilder("prepare")
.setNode(modelMeta.getMeta("prepare"))
.setNode("data", modelMeta.getMeta("empty"))
.setNode(new MetaBuilder("merge").setValue("mergeName", model.meta().getName() + ".empty"));
.setNode(meta.getMeta("prepare"))
.setNode("data", meta.getMeta("empty"))
.setNode(new MetaBuilder("merge").setValue("mergeName", model.getName() + ".empty"));
model.dependsOn("prepare", emptyCfg, "empty");
return model;
}
private Data<? extends Table> subtract(Data<? extends Table> mergeData, Data<? extends Table> emptyData) {
return DataUtils.combine(mergeData, emptyData, Table.class, mergeData.meta(), (BiFunction<Table, Table, Table>) this::subtract);
}

View File

@ -37,7 +37,7 @@ limitations under the License.
<Insets left="10.0" />
</padding></Label>
<Pane HBox.hgrow="ALWAYS" />
<ToggleButton fx:id="processManagerButton" mnemonicParsing="false" text="ProcessManager" />
<!--<ToggleButton fx:id="processManagerButton" mnemonicParsing="false" text="ProcessManager" />-->
<ToggleButton fx:id="consoleButton" contentDisplay="CENTER" mnemonicParsing="false" text="Console" />
</items>
</ToolBar>