Skip to content

Commit

Permalink
Fixed a bug causing sporadic errors in timegap calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
luistar committed May 11, 2021
1 parent f7f1fee commit ab9bf19
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.awt.Label;

import javax.swing.ToolTipManager;

import org.knime.core.node.defaultnodesettings.DefaultNodeSettingsPane;
import org.knime.core.node.defaultnodesettings.DialogComponentFileChooser;
import org.knime.core.node.defaultnodesettings.DialogComponentNumberEdit;
import org.knime.core.node.defaultnodesettings.DialogComponentString;
import org.knime.core.node.defaultnodesettings.DialogComponentStringSelection;
import org.knime.core.node.defaultnodesettings.SettingsModelIntegerBounded;
import org.knime.core.node.defaultnodesettings.SettingsModelString;

/**
Expand All @@ -22,13 +26,26 @@
*/
public class RouteStepAnalyzerNodeDialog extends DefaultNodeSettingsPane {


private static final String MIN_TIME_TOOLTIP = "<html>"
+ "This value (seconds) is used to determine wether two subsegquent visit to the same"
+ "<br>visit to the same segment should be considered as distinct.</br>"
+ "<br>Visits that are less than this time apart from the last valid visit will be discarded.</br>"
+ "</html>";
/**
* New dialog pane for configuring the node. The dialog created here
* will show up when double clicking on a node in KNIME Analytics Platform.
*/
protected RouteStepAnalyzerNodeDialog() {
super();

ToolTipManager.sharedInstance().setDismissDelay(15000);

SettingsModelIntegerBounded minTimeBetween = RouteStepAnalyzerNodeModel.createMinTimeSettings();
DialogComponentNumberEdit minTimeSelector = new DialogComponentNumberEdit(minTimeBetween, "Ignore all visits that are less than this value(seconds) away from the last one : ", 10);
minTimeSelector.setToolTipText(MIN_TIME_TOOLTIP);
minTimeBetween.setEnabled(true);
addDialogComponent(minTimeSelector);

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<li>destination_id, the OSM id of the destination node.</li>
<li>tags, the OSM tags associated to the ways to which the origin and destination nodes both belong.</li>
<li>the_geom, a String representation of a route formatted as WKT Linestring.</li>
The best way to obtain such a table from a specific route is using the Map Matcher Node of the
KNOT toolkit.
</ul>
The best way to obtain such a table is using the Map Matcher Node of the
KNOT toolkit.
</inPort>
<outPort index="0" name="For Day">A table containing the following information:
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.knime.core.node.NodeModel;
import org.knime.core.node.NodeSettingsRO;
import org.knime.core.node.NodeSettingsWO;
import org.knime.core.node.defaultnodesettings.SettingsModelIntegerBounded;
import org.unina.spatialanalysis.RouteStepAnalyzer.RouteStepAnalyzerNodeModel;
import org.unina.spatialanalysis.RouteStepAnalyzer.entity.DetailedDataEntry;
import org.unina.spatialanalysis.RouteStepAnalyzer.entity.SimpleDataEntry;
Expand All @@ -67,9 +68,21 @@
*/
@SuppressWarnings("deprecation")
public class RouteStepAnalyzerNodeModel extends NodeModel {

private static final String MIN_TIME_SETTINGS = "m_min_time";
private static final int DEFAULT_MIN_TIME_SETTINGS = 0;


private static final NodeLogger LOGGER = NodeLogger.getLogger(RouteStepAnalyzerNodeModel.class);

private final SettingsModelIntegerBounded m_minTimeBetween = createMinTimeSettings();


public static SettingsModelIntegerBounded createMinTimeSettings() {
return new SettingsModelIntegerBounded(MIN_TIME_SETTINGS, DEFAULT_MIN_TIME_SETTINGS, 0, Integer.MAX_VALUE);
}


/**
* Constructor for the node model.
*/
Expand Down Expand Up @@ -198,7 +211,7 @@ protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final Ex
}

if(rowId.contains("NEVER_VISITED")) {
Segment s = new Segment(originId, destinationId, tags, theGeom);
Segment s = new Segment(originId, destinationId, tags, theGeom, m_minTimeBetween.getIntValue());
neverVisitedSegmentHolder.addSegment(s);
}else if(ownerId==Integer.MIN_VALUE || theGeom==null || beginAt== null || endAt==null || originId==Long.MIN_VALUE || destinationId == Long.MIN_VALUE) {
LOGGER.info("Row n " + currentRowCounter + " has invalid values! It will be skipped.\n");
Expand All @@ -207,7 +220,7 @@ protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final Ex
if(visitedSegmentHolder.containsSegment(originId, destinationId)) {
s = visitedSegmentHolder.getSegment(originId, destinationId);
}else {
s = new Segment(originId, destinationId, tags, theGeom);
s = new Segment(originId, destinationId, tags, theGeom, m_minTimeBetween.getIntValue());
visitedSegmentHolder.addSegment(s);
}
daysInDataSet.add(beginAt.toLocalDate());
Expand All @@ -224,11 +237,6 @@ protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final Ex

int counter = 0;


while(exec.getProgressMonitor().getProgress()!=0) {
exec.setProgress(0.0);
System.out.println("Do I exit?");
}

for(Segment s: visitedSegmentHolder.getAllSegments()) {
s.generateResults();
Expand All @@ -253,11 +261,6 @@ protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final Ex
}

counter = 0;

while(exec.getProgressMonitor().getProgress()!=0) {
exec.setProgress(0.0);
System.out.println("Do I exit?");
}

for(Segment s: neverVisitedSegmentHolder.getAllSegments()) {
s.generateResults();
Expand Down Expand Up @@ -307,7 +310,15 @@ private void addComplexDataEntry(DetailedDataEntry dEntry, int complexEntryCount
avgDayCell = DataType.getMissingCell();
}
complexCells.add(avgDayCell);


DataCell medianDay;
if(dEntry.getMedianForDay()!=0) {
medianDay= new DoubleCell(dEntry.getMedianForDay());
}else {
medianDay = DataType.getMissingCell();
}
complexCells.add(medianDay);

//Early Morning
DataCell nEmCell = new IntCell(dEntry.getnVisitsEM());
complexCells.add(nEmCell);
Expand All @@ -318,6 +329,15 @@ private void addComplexDataEntry(DetailedDataEntry dEntry, int complexEntryCount
avgEmCell = DataType.getMissingCell();
}
complexCells.add(avgEmCell);
DataCell medianEm;
if(dEntry.getMedianForEM()!=0) {
medianEm= new DoubleCell(dEntry.getMedianForEM());
}else {
medianEm = DataType.getMissingCell();
}
complexCells.add(medianEm);


//Mid Morning
DataCell nMmCell = new IntCell(dEntry.getnVisitsMM());
complexCells.add(nMmCell);
Expand All @@ -328,6 +348,14 @@ private void addComplexDataEntry(DetailedDataEntry dEntry, int complexEntryCount
avgMmCell = DataType.getMissingCell();
}
complexCells.add(avgMmCell);
DataCell medianMm;
if(dEntry.getMedianForMM()!=0) {
medianMm= new DoubleCell(dEntry.getMedianForMM());
}else {
medianMm = DataType.getMissingCell();
}
complexCells.add(medianMm);

//Afternoon
DataCell nACell = new IntCell(dEntry.getnVisistsA());
complexCells.add(nACell);
Expand All @@ -338,6 +366,15 @@ private void addComplexDataEntry(DetailedDataEntry dEntry, int complexEntryCount
avgACell = DataType.getMissingCell();
}
complexCells.add(avgACell);
DataCell medianA;
if(dEntry.getMedianForDay()!=0) {
medianA= new DoubleCell(dEntry.getMedianForA());
}else {
medianA = DataType.getMissingCell();
}
complexCells.add(medianA);


//Evening
DataCell nECell = new IntCell(dEntry.getnVisistsE());
complexCells.add(nECell);
Expand All @@ -348,6 +385,15 @@ private void addComplexDataEntry(DetailedDataEntry dEntry, int complexEntryCount
avgECell = DataType.getMissingCell();
}
complexCells.add(avgECell);
DataCell medianE;
if(dEntry.getMedianForE()!=0) {
medianE= new DoubleCell(dEntry.getMedianForE());
}else {
medianE = DataType.getMissingCell();
}
complexCells.add(medianE);


DataCell tags = new StringCell(dEntry.getTags());
complexCells.add(tags);
DataCell theGeom = new StringCell(dEntry.getTheGeom());
Expand Down Expand Up @@ -377,6 +423,14 @@ private void addSimpleDataEntry(SimpleDataEntry se, int simpleEntryCounter, Buff
avg = DataType.getMissingCell();
}
simpleCells.add(avg);

DataCell median;
if(se.getMedianTime()!=0) {
median= new DoubleCell(se.getMedianTime());
}else {
median = DataType.getMissingCell();
}
simpleCells.add(median);
DataCell tags = new StringCell(se.getTags());
simpleCells.add(tags);
DataCell theGeom = new StringCell(se.getTheGeom());
Expand Down Expand Up @@ -415,6 +469,11 @@ protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws Invali
throw new InvalidSettingsException("The input table must contain the following columns: owner_id, begin_at, end_at, origin_id, destination_id, origin_tags, destination_tags, the_geom");
}

if(m_minTimeBetween.getIntValue()<0) {
throw new InvalidSettingsException("The minimum time between visits cannot be negative!");

}

return new DataTableSpec[] { createOutputForDetailed(),createOutputForSimple() };
}

Expand All @@ -434,26 +493,42 @@ private DataTableSpec createOutputForDetailed() {
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("Day", DateAndTimeCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());

specCreator = new DataColumnSpecCreator("N° Visits(Day)", IntCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("AvgTime(Day)", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("MedianTime(Day)", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());

specCreator = new DataColumnSpecCreator("N° Visits("+TimeSlot.EARLY_MORNING.toString()+")", IntCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("AvgTime("+TimeSlot.EARLY_MORNING.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("MedianTime("+TimeSlot.EARLY_MORNING.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());

specCreator = new DataColumnSpecCreator("N° Visits("+TimeSlot.MID_MORNING.toString()+")", IntCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("AvgTime("+TimeSlot.MID_MORNING.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());;
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("MedianTime("+TimeSlot.MID_MORNING.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());

specCreator = new DataColumnSpecCreator("N° Visits("+TimeSlot.AFTERNOON.toString()+")", IntCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("AvgTime("+TimeSlot.AFTERNOON.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("MedianTime("+TimeSlot.AFTERNOON.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());

specCreator = new DataColumnSpecCreator("N° Visits("+TimeSlot.EVENING.toString()+")", IntCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("AvgTime("+TimeSlot.EVENING.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("MedianTime("+TimeSlot.EVENING.toString()+")", DoubleCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());

specCreator = new DataColumnSpecCreator("Tags", StringCell.TYPE);
newColumnSpecs.add(specCreator.createSpec());
HashMap<String, String> m = new HashMap<String, String>();
Expand Down Expand Up @@ -483,12 +558,10 @@ private DataTableSpec createOutputForSimple() {
simpleColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("N° Visits", IntCell.TYPE);
simpleColumnSpecs.add(specCreator.createSpec());



specCreator = new DataColumnSpecCreator("Avg time Between", DoubleCell.TYPE);
DataColumnSpec e = specCreator.createSpec();
simpleColumnSpecs.add(e);
simpleColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("Median time", DoubleCell.TYPE);
simpleColumnSpecs.add(specCreator.createSpec());
specCreator = new DataColumnSpecCreator("Tags", StringCell.TYPE);
simpleColumnSpecs.add(specCreator.createSpec());
HashMap<String, String> m = new HashMap<String, String>();
Expand Down Expand Up @@ -519,7 +592,7 @@ protected void saveSettingsTo(final NodeSettingsWO settings) {
* all common data types. Hence, you can easily write your settings manually.
* See the methods of the NodeSettingsWO.
*/

m_minTimeBetween.saveSettingsTo(settings);
}

/**
Expand All @@ -534,8 +607,7 @@ protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws I
* The SettingsModel will handle the loading. After this call, the current value
* (from the view) can be retrieved from the settings model.
*/
;

m_minTimeBetween.loadSettingsFrom(settings);

}

Expand All @@ -550,6 +622,7 @@ protected void validateSettings(final NodeSettingsRO settings) throws InvalidSet
* already handled in the dialog. Do not actually set any values of any member
* variables.
*/
m_minTimeBetween.validateSettings(settings);

}

Expand Down
Loading

0 comments on commit ab9bf19

Please sign in to comment.