Skip to content

Commit

Permalink
Fix saccade velocity sorting and add tests
Browse files Browse the repository at this point in the history
I don't actually like this implementation. Future programmer, see the associated issue in the PR for more explanations. I am lost and didn't get a response on what to do from the boss
  • Loading branch information
ashkjones committed Jul 30, 2024
1 parent 3e45254 commit 2fe5458
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/github/thed2lab/analysis/SaccadeVelocity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@ static public LinkedHashMap<String,String> analyze(DataEntry data) {
if (!prevID.equals(currID) && positionProfile.size() != 0) {
positionProfiles.add(positionProfile);
positionProfile = new ArrayList<Double[]>();
prevID = currID;
}

Double x = Double.parseDouble(data.getValue(FIXATIONX_INDEX, i));
Double y = Double.parseDouble(data.getValue(FIXATIONY_INDEX, i));
Double t = Double.parseDouble(data.getValue(TIME_INDEX, i));
positionProfile.add(new Double[] {x, y, t});
prevID = currID;

} else if (positionProfile.size() != 0) {
positionProfiles.add(positionProfile);
positionProfile = new ArrayList<Double[]>();
}
}

// add the last saccade profile if it isn't already added
// see issue #39 and #40 for explanation
if (positionProfile.size() > 0) {
positionProfiles.add(positionProfile);
}

for (int i = 0; i < positionProfiles.size(); i++) {
Expand All @@ -56,7 +62,7 @@ static public LinkedHashMap<String,String> analyze(DataEntry data) {
}


/*
/**
* Returns the peak velocity of a given saccade calculated using a two point central difference algorithm.
*
* @param saccadePoints A list of saccade data points, where each data point is a double array.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.github.thed2lab.analysis;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.Test;

public class SaccadeVelocityTest {

final double PRECISION = 0.000000001; // allowable floating point error
final static int SCREEN_WIDTH = 1920;
final static int SCREEN_HEIGHT = 1080;

@Test
public void testGetPeakVelocity_emptyPoints_returnNaN() {
List<Double[]> emptyList = new ArrayList<>();
boolean isNaN = Double.isNaN(SaccadeVelocity.getPeakVelocity(emptyList));
assertTrue(isNaN);
}

@Test
public void testGetPeakVelocity_onePoint_returnNaN() {
List<Double[]> onePoint = new ArrayList<>() {{
add(new Double[]{1., 1., 0.001});
}};
boolean isNaN = Double.isNaN(SaccadeVelocity.getPeakVelocity(onePoint));
assertTrue(isNaN);
}

@Test
public void testGetPeakVelocity_aboveThreshold_return0() {
List<Double[]> saccadePoints = new ArrayList<>() {{
add(new Double[]{0., 0., 0.});
add(new Double[]{0., 10., 0.0001});
}};
// 2332.21914 > 700 => return 0
boolean isZero = SaccadeVelocity.getPeakVelocity(saccadePoints) == 0;
assertTrue(isZero);
}

@Test
public void testGetPeakVelocity_normalUseCase_returnVelocityValue() {

}

@Test
public void testSaccadeVelocityAnalyze_nonContinuousEndsWithSaccadeData() {
final String GAZE_PATH = "./src/test/resources/filtered_by_validity.csv";
final double EXPECTED_AVG_PEAK = 169.15239533934;
final String KEY = "average_peak_saccade_velocity";
DataEntry gazeData = DataFilter.applyScreenSize(FileHandler.buildDataEntry(new File(GAZE_PATH)), SCREEN_WIDTH, SCREEN_HEIGHT);

Map<String, String> actualMap = SaccadeVelocity.analyze(gazeData);
assertEquals(1, actualMap.size());
assertEquals(EXPECTED_AVG_PEAK, Double.parseDouble(actualMap.get(KEY)), PRECISION);

}

@Test
public void testSaccadeVelocityAnalyze_nonContinuousEndsWithFixationData() {
final String GAZE_PATH = "./src/test/resources/filtered_by_validity.csv";
final double EXPECTED_AVG_PEAK = 169.15239533934;
final String KEY = "average_peak_saccade_velocity";
DataEntry gazeData = DataFilter.applyScreenSize(FileHandler.buildDataEntry(new File(GAZE_PATH)), SCREEN_WIDTH, SCREEN_HEIGHT);
gazeData.process(Arrays.asList(
"0","X-Plane","971","6.53005","5.88E+12","0.47815","0.45335",
"6.53005","0.14856","25","1","0.46056","0.45713","1","0.60729",
"0.47315","0"," ","0","","0.41066","0.36362","16.25439","1.08789",
"1","0.61855","0.37037","15.69818","1.08789","1","0","0","11",
"4.43149","1","4.03482","1","0","0","0","0","0","0","0","0","1",
"1","1","1","1","1","1","0","0","0","AOI_A","0","0","0"
));

Map<String, String> actualMap = SaccadeVelocity.analyze(gazeData);
assertEquals(1, actualMap.size());
assertEquals(EXPECTED_AVG_PEAK, Double.parseDouble(actualMap.get(KEY)), PRECISION);

}
}

0 comments on commit 2fe5458

Please sign in to comment.