Skip to content

Commit

Permalink
implement some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Neise committed Jan 20, 2017
1 parent 67011be commit 6359d71
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<fact.extraction.SinglePulseExtraction
dataKey="DataCalibrated"
outputKey="PhotonArrivals"
url="${integralGainFile}"
/>

<fact.utils.ConvertSinglePulses2Timeseries
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/fact/extraction/SinglePulseExtraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Arrays;
import fact.features.singlePulse.timeSeriesExtraction.SinglePulseExtractor;
import fact.features.singlePulse.timeSeriesExtraction.ElementWise;
import fact.features.singlePulse.timeSeriesExtraction.TemplatePulse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import stream.io.SourceURL;
Expand Down Expand Up @@ -99,6 +100,7 @@ public class SinglePulseExtraction implements Processor {
protected SourceURL url = null;

protected double[] integralGains = null;
protected double factSinglePePulseIntegral;

@Override
public Data process(Data input) {
Expand Down Expand Up @@ -131,7 +133,10 @@ public Data process(Data input) {
);

double[] pixelTimeSeries = ElementWise.multiply(
pixelTimeSeriesInMv, 24.37/integralGains[pix]);
pixelTimeSeriesInMv,
factSinglePePulseIntegral /
integralGains[pix]
);

SinglePulseExtractor.Result result = spe.extractFromTimeSeries(
pixelTimeSeries);
Expand Down Expand Up @@ -166,6 +171,8 @@ private void addStartSliceOffset(int[][] arr) {
}

public double[] loadIntegralGainFile(SourceURL inputUrl, Logger log) {
factSinglePePulseIntegral = TemplatePulse.factSinglePePulseIntegral();

double[] integralGains = new double[npix];
Data integralGainData = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ public class TemplatePulse {
* | | /
* 0 lengthInSlices
*/
public static double[] factSinglePePulse(int lengthInSlices) {

final double periodeSliceInNs = 0.5;
public static double[] factSinglePePulse(int lengthInSlices, double samplingPeriodInNs) {

final double[] time = new double[lengthInSlices];
for (int i = 0; i < time.length; i++) {time[i] = i*periodeSliceInNs;}
for (int i = 0; i < time.length; i++) {
time[i] = i * samplingPeriodInNs;
}

double[] template = new double[lengthInSlices];
for (int i = 0; i < time.length; i++) {

final double amplitude =
final double amplitude =
1.626*
(1.0-Math.exp(-0.3803*time[i]))
*Math.exp(-0.0649*time[i]);

if(amplitude < 0.0) {
template[i] = 0.0;
}else{
Expand All @@ -65,4 +65,70 @@ public static double[] factSinglePePulse(int lengthInSlices) {
}
return template;
}

public static double[] factSinglePePulse(int lengthInSlices) {

final double samplingPeriodInNs = 0.5;
return factSinglePePulse(lengthInSlices, samplingPeriodInNs);

}

public static double factSinglePePulseIntegral() {

final double half_height = 0.5; // this is fix forever :-D
final double samplingPeriodInNs = 1e-3;
final int integrationWindowLengthInSlices = 30;
final double sliceInNs = 0.5;
final int factor = (int)(sliceInNs / samplingPeriodInNs);
final double pulseLengthInNs = integrationWindowLengthInSlices/sliceInNs + 3.;
final int templateLength = (int) (pulseLengthInNs / samplingPeriodInNs);
double[] template = factSinglePePulse(templateLength, samplingPeriodInNs);

// resample in 500ps steps, i.e. take only every 500th sample
double[] sums = new double[factor];
double[] resampled_pulse;
for (int start_offset = 0; start_offset < factor; start_offset++){
resampled_pulse = new double[templateLength/factor];
for (int i=0; i<resampled_pulse.length; i++){
resampled_pulse[i] = template[start_offset+(i*factor)];
}

sums[start_offset] = integrate(
resampled_pulse,
findHalfHeightPosition(resampled_pulse, 1.),
integrationWindowLengthInSlices
);
}

return mean(sums);

}

public static int findHalfHeightPosition(double[] samples, double height) {
for (int i= 1; i < samples.length; i++){
if (samples[i] > height/2.) {
return i - 1;
}
}
return samples.length;
}

public static double integrate(double[] samples, int start, int length) {
double sum = 0;
for (int i=0; i < length; i++) {
sum += samples[start+i];
}
return sum;
}

public static double mean(double[] foo) {
if (foo.length == 0) { return Double.NaN; }

double m = 0.;
for (double a: foo){
m += a;
}
return m / foo.length;
}

}
58 changes: 58 additions & 0 deletions src/test/java/fact/utils/TemplatePulseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fact.utils;

import fact.features.singlePulse.timeSeriesExtraction.TemplatePulse;
import junit.framework.Assert;
import org.junit.Test;

public class TemplatePulseTest {

@Test
public void testIfResultNear24() {

double result = TemplatePulse.factSinglePePulseIntegral();
System.out.println(result);
Assert.assertTrue(
Math.abs(result - 24.37) < 0.01
);
}

@Test
public void testIntegrateEmptySamples() {
Throwable e = null;
try {
double[] samples = new double[]{};
TemplatePulse.integrate(samples, 0, 10);
}catch(Throwable ex) {
e = ex;
}
Assert.assertTrue(e instanceof IndexOutOfBoundsException);
}

@Test
public void testIntegrate() {
double[] samples = new double[]{1,2,3};
Assert.assertEquals(
6.,
TemplatePulse.integrate(samples, 0, 3)
);
}

@Test
public void testMean() {
double[] foo = new double[]{1,2,3};
Assert.assertEquals(
2.,
TemplatePulse.mean(foo)
);
}

@Test
public void testfindHalfHeightPosition() {
double[] foo = new double[]{0.1, 0.3, 0.7, 1, 1, 0.5, 0.3};
Assert.assertEquals(
1,
TemplatePulse.findHalfHeightPosition(foo, 1.)
);
}

}

0 comments on commit 6359d71

Please sign in to comment.