Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into real-reducer-modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
markhalonen committed Aug 29, 2018
2 parents d3121c6 + c202087 commit ff15a98
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
Expand Down
8 changes: 5 additions & 3 deletions Libraries/src/net/relinc/libraries/application/BarSetup.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.relinc.libraries.application;

import java.io.File;
import java.util.UUID;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
Expand All @@ -23,15 +24,16 @@ public BarSetup(Bar incid, Bar trans){
public BarSetup(String path) {
//path is a .zip file.
//these are temp, no working directory //TODO: This aint that sweet
File incidentDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/Temp/Incident Bar");
File tranmissionDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/Temp/Transmission Bar");
String uuid = UUID.randomUUID().toString();
File incidentDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Incident Bar");
File tranmissionDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid + "/Transmission Bar");
SPOperations.deleteFolder(incidentDir);
SPOperations.deleteFolder(tranmissionDir);
String fullName = new File(path).getName(); //has .zip
name = fullName.substring(0, fullName.length() - 4);
try {
ZipFile zipFile = new ZipFile(path);
zipFile.extractAll(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/Temp");
zipFile.extractAll(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp/" + uuid);
} catch (ZipException e) {
e.printStackTrace();
}
Expand Down
8 changes: 0 additions & 8 deletions Libraries/src/net/relinc/libraries/data/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@ public Dataset(double[] time, double[] d){
data = d;
}

public double[] getDerivative(){
double[] derivative = new double[data.length];
for(int i = 0; i < derivative.length - 1; i++){
derivative[i] = (data[i + 1] - data[i]) / (timeData[i + 1] - timeData[1]);
}
return derivative;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ else if(activated.get()){
else {
return fullData;
}
// if(SPSettings.globalLoadDataLowpassFilter != null) //global overrides.
// return SPMath.fourierLowPassFilter(fullData, SPSettings.globalLoadDataLowpassFilter.getLowPassValue(), 1 / (activatedData.Data.timeData[1] - activatedData.Data.timeData[0]));
// else if(activated.get())
// return SPMath.fourierLowPassFilter(fullData, lowPassValue, 1 / (activatedData.Data.timeData[1] - activatedData.Data.timeData[0]));
// else
// return fullData;
}

@Override
Expand Down
137 changes: 137 additions & 0 deletions Libraries/src/net/relinc/libraries/dev/TaskMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package net.relinc.libraries.dev;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class TaskMonitor {

private static List<Task> tasks = new ArrayList<Task>();
private static int taskCount = 0;
public static int start(String taskName) {
tasks.add(new Task(taskName, ++taskCount));
return taskCount;
}

public static void end(int taskId) {
Task endTask = tasks.stream().filter(t -> t.getId() == taskId).findFirst().get();
endTask.end();
}

public static void printTasks(String orderBy) {
System.out.println("==================================================================");
System.out.println("Tasks: " + taskCount + " ordered by: " + orderBy);
List<Task> sortedTasks = Collections.emptyList();
switch(orderBy) {
case "duration":
sortedTasks = tasks.stream().sorted((a, b) -> a.getDuration().compareTo(b.getDuration())).collect(Collectors.toList());
break;
case "start":
sortedTasks = tasks.stream().sorted((a, b) -> a.getStartTime().get().compareTo(b.getStartTime().get())).collect(Collectors.toList());
break;
default:
throw new RuntimeException("orderBy: " + orderBy + " is not supported!");
}

sortedTasks.stream().forEach(task -> {
System.out.println(task);
});
System.out.println("==================================================================");
}

public static void printTimeline() {
Map<Instant, Task> allInstants = new HashMap<Instant, Task>();
tasks.stream().forEach(task -> {
allInstants.put(task.getStartTime().get(), task);
allInstants.put(task.getEndTime().get(), task);
});
allInstants.entrySet().stream().sorted((a,b) -> a.getKey().compareTo(b.getKey())).forEach(entry -> {
Task task = entry.getValue();
if(entry.getKey().equals(task.getStartTime().get())) {
System.out.println(String.format("Start task=%s, id=%d", task.getName(), task.getId()));
} else {
System.out.println(String.format("End task=%s id=%d duration=%.1f", task.getName(), task.getId(), task.getDuration().toMillis() * .001));
}
});
}
}

class Task {
private Optional<Instant> startTime = Optional.empty(); // using `null` is for noobs
private Optional<Instant> endTime = Optional.empty();
private String name = "";
private int id = -1;

public Task(String name, int id) {
this.name = name;
this.id = id;
this.start();
}

public void start() {
this.setStartTime(java.time.Instant.now());
}

public void end() {
this.setEndTime(java.time.Instant.now());
}

public Optional<Instant> getStartTime() {
return startTime;
}
public void setStartTime(Instant startTime) {
this.startTime = Optional.of(startTime);
}
public Optional<Instant> getEndTime() {
return endTime;
}
public void setEndTime(Instant endTime) {
this.endTime = Optional.of(endTime);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public void checkStartEndPresent() {
if(!(this.getStartTime().isPresent() && this.getEndTime().isPresent()))
throw new RuntimeException("Cannot getDuration if end or start is not set");
}

public Duration getDuration() {
checkStartEndPresent();
return Duration.between(this.getStartTime().get(), this.getEndTime().get());
}

@Override
public String toString() {
String res = "Task\n";
res += String.format("id=%d\n", this.getId());
res += String.format("name=%s\n", this.getName());
boolean startPresent = this.getStartTime().isPresent();
boolean endPresent = this.getEndTime().isPresent();
if(startPresent && endPresent)
res += String.format("duration (s)=%.1f\n", this.getDuration().toMillis() * .001);
if(startPresent)
res += String.format("start=%s\n", this.getStartTime().get().toEpochMilli());
if(endPresent)
res += String.format("end=%s\n", this.getEndTime().get().toEpochMilli());
res += "\n";
return res;
}

}
53 changes: 36 additions & 17 deletions Libraries/src/net/relinc/libraries/staticClasses/SPOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.imageio.ImageIO;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
Expand Down Expand Up @@ -163,10 +160,10 @@ public static String readStringFromFile(String path){
}
return text;
}

public static void deleteFolder(File folder) {

File[] files = folder.listFiles();
private static void deleteFolder(File folder, boolean deleteFolderToo)
{
File[] files = folder.listFiles();
if(files!=null) { //some JVMs return null for empty dirs
for(File f: files) {
if(f.isDirectory()) {
Expand All @@ -176,7 +173,18 @@ public static void deleteFolder(File folder) {
}
}
}
folder.delete();
if(deleteFolderToo)
folder.delete();
}

public static void deleteFolderContents(File folder)
{
deleteFolder(folder, false);
}

public static void deleteFolder(File folder)
{
deleteFolder(folder, true);
}

public static void findFiles(File dir, TreeItem<String> parent, TreeView<String> treeView, String folderPath, String filePath) {
Expand Down Expand Up @@ -298,8 +306,8 @@ public static void copyFolder(File src, File dest)
}

public static String getSampleType(String samplePath){
//Sample sample = null;
File tempUnzippedSample = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse" + "/TempUnzipLocation2");
String uuid = UUID.randomUUID().toString();
File tempUnzippedSample = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse" + "/tmp/" + uuid);
ZipFile zippedSample = null;
try {
zippedSample = new ZipFile(samplePath);
Expand All @@ -322,12 +330,12 @@ public static String getSampleType(String samplePath){
String parametersString = SPOperations.readStringFromFile(tempUnzippedSample + "/Parameters.txt");

return getSampleTypeFromSampleParametersString(parametersString);

}

public static Sample loadSampleParametersOnly(String samplePath) throws ZipException{
Sample sample = null;
File tempUnzippedSample = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse" + "/TempUnzipLocation");
String uuid = UUID.randomUUID().toString();
File tempUnzippedSample = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse" + "/tmp/" + uuid);
ZipFile zippedSample = new ZipFile(samplePath);
if(tempUnzippedSample.exists())
SPOperations.deleteFolder(tempUnzippedSample);
Expand Down Expand Up @@ -370,16 +378,18 @@ public static Sample loadSampleParametersOnly(String samplePath) throws ZipExcep
sample.setDescriptorsFromString(descriptors);
}

SPOperations.deleteFolder(tempUnzippedSample);
return sample;
}

public static Sample loadSample(String samplePath) throws Exception { //samplePath is a zipfile.
Sample sample = null;
File tempUnzippedSample = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse" + "/TempUnzipLocation");
String uuid = UUID.randomUUID().toString();
File tempUnzippedSample = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse" + "/tmp/" + uuid);
ZipFile zippedSample = new ZipFile(samplePath);
if(tempUnzippedSample.exists())
SPOperations.deleteFolder(tempUnzippedSample);
tempUnzippedSample.mkdir();
tempUnzippedSample.mkdirs();

zippedSample.extractAll(tempUnzippedSample.getPath());

Expand Down Expand Up @@ -439,7 +449,6 @@ public static Sample loadSample(String samplePath) throws Exception { //samplePa
e.printStackTrace();
}

//SPOperations.deleteFolder(tempUnzippedSample);
return sample;
}

Expand Down Expand Up @@ -487,6 +496,16 @@ public static void prepareAppDataDirectory(){
if(!tempFileDir.exists()){
System.out.println(tempFileDir.mkdir());
}

// For speed reasons, we leave things in tmp and clean it all at once at the beginning.
File tmpDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/tmp");
if(tmpDir.exists())
{
deleteFolderContents(tmpDir);
} else {
tmpDir.mkdirs();
}

File tempSampleDataDir = new File(SPSettings.applicationSupportDirectory + "/RELFX/SUREPulse/TempSampleData");

SPOperations.deleteFolder(tempSampleDataDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package net.relinc.libraries.staticClasses;

import java.util.stream.IntStream;


public final class TriangularLowPassFilter {

private static double findTriangularMovingAverage(double[] data, double f_t, int idx) {
int range = (int) (.25 / f_t);
private static double findTriangularMovingAverage(double[] data, int range, int idx) {
double value = 0;
for (int idxFilter = -range; idxFilter < range; idxFilter++) {
double weight = range - Math.abs(idxFilter);
if (idx + idxFilter <= 0) {
value += data[0] * (range - Math.abs(idxFilter));
value += data[0] * weight;
} else if (idx + idxFilter >= data.length - 1) {
value += data[data.length - 1] * (range - Math.abs(idxFilter));
value += data[data.length - 1] * weight;
} else {
value += data[idx + idxFilter] * (range - Math.abs(idxFilter));
value += data[idx + idxFilter] * weight;
}
}
return value;
}

public static double[] triangularLowPass(double[] data, double f_t) {
public static double[] triangularLowPass(double[] data, double lowpassToFrequencyRatio) {

double[] filtered_data = new double[data.length];
int range = (int) (.25 / f_t);
int range = (int) (.25 / lowpassToFrequencyRatio);

double total = 0;

for (int idxFilter = -range; idxFilter < range; idxFilter++) {
total += range - Math.abs(idxFilter);
}

for (int idx = 0; idx < data.length; idx++) {
filtered_data[idx] = findTriangularMovingAverage(data, f_t, idx) / total;
}

final double totalFinal = total;
filtered_data = IntStream.range(0, data.length)
.parallel() // empirically, 4-5X faster.
.mapToDouble(idx -> findTriangularMovingAverage(data, range, idx) / totalFinal).toArray();
return filtered_data;
}
}
Loading

0 comments on commit ff15a98

Please sign in to comment.