diff --git a/Jenkinsfile b/Jenkinsfile index e5b8a508..94bd378f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,7 +5,7 @@ node { // set environment variables env.ANDROID_SDK_ROOT = '/var/lib/android-sdk' - env.JAVA_HOME = '/opt/openjdk-bin-17' + env.JAVA_HOME = '/opt/openjdk-bin-21' env.PATH = "${env.JAVA_HOME}/bin:${env.PATH}" // checkout code from scm diff --git a/build.gradle b/build.gradle index 2ac992eb..e18abef9 100644 --- a/build.gradle +++ b/build.gradle @@ -61,7 +61,7 @@ allprojects { // locoslab libraries and code versionGeometry = '1.4.0' versionCarta = '1.3.0' - versionGrib2 = '1.0.0' + versionGrib2 = '1.0.1' versionAuth = '1.1.0' // tools versionCli = '1.4' @@ -76,7 +76,7 @@ subprojects { afterEvaluate { java { toolchain { - languageVersion = JavaLanguageVersion.of(17) + languageVersion = JavaLanguageVersion.of(21) } } } diff --git a/service-tool/src/main/java/com/mowesta/tool/paper/DeviceTemperatureEval.java b/service-tool/src/main/java/com/mowesta/tool/paper/DeviceTemperatureEval.java index 865faae4..8bcad3a1 100644 --- a/service-tool/src/main/java/com/mowesta/tool/paper/DeviceTemperatureEval.java +++ b/service-tool/src/main/java/com/mowesta/tool/paper/DeviceTemperatureEval.java @@ -29,7 +29,7 @@ import com.mowesta.tool.paper.LocationClassifier.TripResult; public class DeviceTemperatureEval { - public static final File folder = new File("mowesta-dump"); + public static final File folder = new File("/Users/Marcus/git/mowesta-data-and-other/mowesta-dump"); public static void main(String[] args) throws Exception { // hunga tonga vulcano diff --git a/service-tool/src/main/java/com/mowesta/tool/paper/PrepareDataForTorch.java b/service-tool/src/main/java/com/mowesta/tool/paper/PrepareDataForTorch.java new file mode 100644 index 00000000..1b1a63b0 --- /dev/null +++ b/service-tool/src/main/java/com/mowesta/tool/paper/PrepareDataForTorch.java @@ -0,0 +1,286 @@ +package com.mowesta.tool.paper; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.locoslab.library.geometry.wgs.HaversineDistance; +import com.mowesta.api.Coordinate; +import com.mowesta.api.device.Device; +import com.mowesta.api.device.OperatingSystem; +import com.mowesta.api.measurement.Measurement; +import com.mowesta.tool.analysis.WeatherResolver; +import com.mowesta.tool.analysis.WeatherResolver.DataType; +import com.mowesta.tool.analysis.WeatherResolver.Resolution; +import com.mowesta.tool.paper.Algo.ErrorSummary; +import com.mowesta.tool.paper.LocationClassifier.TripResult; + +public class PrepareDataForTorch { + + public static class MeasurementData { + public double temperature; + public long timestamp; + public double distance; + public boolean active; + } + + + public static class TripData { + public long timestamp; + public double temperature; + public List measurements = new ArrayList(); + } + + + public static class DeviceData { + public long id; + public String model; + public String manufacturer; + public List trips = new ArrayList(); + } + + public static class ExperimentData { + public List devices = new ArrayList(); + } + + + + public static final File folder = new File("/Users/Marcus/git/mowesta-data-and-other/mowesta-dump"); + + public static void main(String[] args) throws Exception { + // hunga tonga vulcano + long start = getDate("2019-01-01 0:00"); + long end = getDate("2023-01-01 00:00"); + ObjectMapper mapper = new ObjectMapper(); + ExperimentData result = new ExperimentData(); + + List devices = readFile(new File(folder, "devices.json"), Device.class); + for (Device d: devices) { + if (d.getId() == 24963415l) continue; // stationary tablet (will cause out-of-memory) + if (d.getId() == 20488695l) { +// System.out.println("Dev: " + d.getManufacturer() + " " + d.getModel()); + } + if (d.getId() == 20488695l) continue; // device with 1 degree precision + if (d.getId() == 35723568l) continue; // t: 0.9 - 9.5 + if (d.getId() == 40549928l) continue; // one of our test devices + + + //if (d.getId() != 11626535l) continue; + if (d.getOs() == OperatingSystem.ANDROID) { + List ms = readFile(new File(folder, "measurement-" + d.getId() + ".json" ), Measurement.class); + ms = ms.stream().filter(m->m.getTime() > start && m.getTime() < end).collect(Collectors.toList()); + Collections.sort(ms, (a, b) -> Long.compare(a.getTime(), b.getTime())); + List trips = getTrips(ms, System.currentTimeMillis()); + List walking = trips.stream().filter(t -> t.getDistance() > 300 && t.getSpeed() < 2 && t.getDuration() > 5 * 60 * 1000).collect(Collectors.toList()); + List walkingWithTruth = walking.stream().filter(t -> getStationTemperature(t) != null).collect(Collectors.toList()); + if (walking.size() > 100 && walkingWithTruth.size() > 0) { + DeviceData dev = new DeviceData(); + dev.id = d.getId(); + dev.model = d.getModel(); + dev.manufacturer = d.getManufacturer(); + result.devices.add(dev); + for (TripResult t: walkingWithTruth) { + TripData trip = new TripData(); + trip.timestamp = t.origin.getTime(); + trip.temperature = getStationTemperature(t); + dev.trips.add(trip); + Measurement previous = t.origin; + for (Measurement m : t.motion) { + Double temperature = Mse.getDeviceTemperature(m); + if (temperature == null) continue; + Boolean active = Mse.isActive(m); + MeasurementData md = new MeasurementData(); + md.distance = HaversineDistance.getDistance(previous.getCoordinate().getLongitude(), previous.getCoordinate().getLatitude(), m.getCoordinate().getLongitude(), m.getCoordinate().getLatitude()); + md.temperature = temperature; + md.timestamp = m.getTime(); + md.active = active; + previous = m; + trip.measurements.add(md); + } + } + } + } + } + mapper.writeValue(new File("output.json"), result); + + } + + public static void appendError(StringBuilder b, List map) { + ErrorSummary s = Algo.errors(map); + append(b, s.mae); + append(b, s.rms); + append(b, s.max); + for (int i = 0; i< s.distribution.length; i++) { + //append(b, s.distribution[i]); + } + } + + public static void append(StringBuilder b, double value) { + b.append(("" + value).replace(".", ",") + ";"); + } + + public static List getAllPairs(List ms) { + List result = new ArrayList<>(); + for (Measurement m: ms) { + Double station = Mse.getStationTemperature(m); + Double device = Mse.getDeviceTemperature(m); + if (station != null && device != null) { + result.add(new double[] { device, station }); + } + } + return result; + } + + public static List getTripPairs(List trips) { + List result = new ArrayList<>(); + for (TripResult r: trips) { + for (Measurement m: r.motion) { + Double station = Mse.getStationTemperature(m); + Double device = Mse.getDeviceTemperature(m); + if (station != null && device != null) { + result.add(new double[] { device, station }); + } + } + } + return result; + } + + + + + public static List getPairs(List walking, double time, boolean noActive) { + List outside = new ArrayList<>(); + walking.forEach(t -> { + double station = getStationTemperature(t); + Double temperature = getDeviceTemperatureAt(t, time, noActive); + if (temperature != null) { + outside.add(new double[] { temperature, station }); + } + }); + return outside; + } + + + public static Double getDeviceTemperatureAt(TripResult t, double minute, boolean noActive) { + Measurement first = t.origin; + double firstMinute = 0; + Measurement last = t.destination; + double lastMinute = getMinuteForMeasurement(t.origin, last); + if (minute > lastMinute) { + return null; + } + for (Measurement m: t.motion) { + double curr = getMinuteForMeasurement(t.origin, m); + if (curr <= minute) { + if (minute - curr < minute - firstMinute) { + first = m; + firstMinute = curr; + } + } else { + if (curr - minute < lastMinute - minute) { + last = m; + lastMinute = curr; + } + } + } + if (noActive) { + // filter trips with active measurements + if (Mse.isActive(last) || Mse.isActive(first)) return null; + int count = 1; + for (Measurement m: t.motion) { + if (Mse.isActive(m)) { + count -= 1; + if (count == 0) + return null; + } + if (m == last) break; + } + } + // linear interpolation + double ft = Mse.getDeviceTemperature(first); + double lt = Mse.getDeviceTemperature(last); + if (lastMinute == firstMinute) return ft; + double percent = (minute - firstMinute) / (lastMinute - firstMinute); + //System.out.println("perc: " + percent); + double res = percent * lt + (1 - percent) * ft; + //System.out.println("FT: " + ft + " LT: " + lt + " Timing: " + firstMinute + " " + lastMinute + " " + minute + " = " + res); + return res; + } + + public static double getMinuteForMeasurement(Measurement origin, Measurement m) { + return (m.getTime() - origin.getTime()) / 60000d; + } + + public static Double getStationTemperature(TripResult t) { + Double r = Mse.getStationTemperature(t.origin); + if (r == null) { + for (Measurement m: t.motion) { + r = Mse.getStationTemperature(m); + if (r != null) break; + } + if (r == null) { + //r = Mse.getStationTemperature(t.destination); + } + } + return r; + } + + + public static List getTrips(List ms, long now) { + List results = new ArrayList<>(); + TripResult result; + while ((result = LocationClassifier.computeNextTrip(ms, now)) != null) { + results.add(result); + ms = result.remainder; + } + return results; + } + + public static Measurement getMeasurement(TripResult result, double minute, double threshold) { + List candidates = result.motion.stream().filter(m -> { + double min = (m.getTime() - result.origin.getTime()) / 60000d; + return min > minute - 0.5 * threshold && min <= minute + 0.5 * threshold; + }).collect(Collectors.toList()); + if (candidates.isEmpty()) return null; + else return candidates.get(0); + } + + + public static String toString(Date date) { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + return df.format(date); + } + + public static long getDate(String string) throws ParseException { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + Date date = df.parse(string); + return date.getTime(); + } + + + public static List readFile(File file, Class type) throws Exception { + ObjectMapper mapper = new ObjectMapper(); + List result = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8))) { + String line = null; + while ((line = reader.readLine()) != null) { + result.add(mapper.readValue(line, type)); + } + } + return result; + } + + +}