Search in sources :

Example 16 with LibreTrendPoint

use of com.eveningoutpost.dexdrip.utils.LibreTrendPoint in project xDrip by NightscoutFoundation.

the class LibreAlarmReceiver method processReadingDataTransferObject.

public static void processReadingDataTransferObject(ReadingData readingData, long CaptureDateTime, String tagid, boolean allowUpload, byte[] patchUid, byte[] patchInfo) {
    Log.d(TAG, "Data that was recieved from librealarm is " + HexDump.dumpHexString(readingData.raw_data));
    // Save raw block record (we start from block 0)
    LibreBlock libreBlock = LibreBlock.createAndSave(tagid, CaptureDateTime, readingData.raw_data, 0, allowUpload, patchUid, patchInfo);
    if (Pref.getBooleanDefaultFalse("external_blukon_algorithm")) {
        if (readingData.raw_data == null) {
            Log.e(TAG, "Please update LibreAlarm to use OOP algorithm");
            JoH.static_toast_long(gs(R.string.please_update_librealarm_to_use_oop_algorithm));
            return;
        }
        LibreOOPAlgorithm.sendData(readingData.raw_data, CaptureDateTime, tagid);
        return;
    }
    LibreTrendUtil libreTrendUtil = LibreTrendUtil.getInstance();
    // Get the data for the last 24 hours, as this affects the cache.
    List<LibreTrendPoint> libreTrendPoints = libreTrendUtil.getData(JoH.tsl() - Constants.DAY_IN_MS, JoH.tsl(), true);
    readingData.ClearErrors(libreTrendPoints);
    boolean use_smoothed_data = Pref.getBooleanDefaultFalse("libre_use_smoothed_data");
    if (use_smoothed_data) {
        readingData.calculateSmoothDataImproved(libreTrendPoints);
    }
    CalculateFromDataTransferObject(readingData, use_smoothed_data, true);
}
Also used : LibreTrendUtil(com.eveningoutpost.dexdrip.utils.LibreTrendUtil) LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock) LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint)

Example 17 with LibreTrendPoint

use of com.eveningoutpost.dexdrip.utils.LibreTrendPoint in project xDrip by NightscoutFoundation.

the class LibreTrendUtil method getData.

public List<LibreTrendPoint> getData(long startTimestamp, long endTimestamp, boolean calculate_factor) {
    Log.i(TAG, "getData called startTimestamp = " + JoH.dateTimeText(startTimestamp) + " endTimestamp = " + JoH.dateTimeText(endTimestamp) + " Size of array is " + m_points.size() + " this = " + this + " m_libreTrendLatest.timestamp " + JoH.dateTimeText(m_libreTrendLatest.timestamp));
    long startTime = Math.max(startTimestamp, m_libreTrendLatest.timestamp);
    // The extra 1 is to make sure we don't read the last packet again and again.
    List<LibreBlock> latestBlocks = LibreBlock.getForTrend(startTime + 1, endTimestamp);
    Log.i(TAG, "Size of latestBlocks is " + latestBlocks.size());
    if (latestBlocks.size() > 0) {
        Log.i(TAG, "Last packet timestamp is " + latestBlocks.get(latestBlocks.size() - 1).timestamp);
    }
    if (calculate_factor) {
        CalculateFactor(latestBlocks);
    }
    // Go over all blocks from the earlier to the latest, and fill the data.
    for (LibreBlock libreBlock : latestBlocks) {
        AddLibreblock(libreBlock);
    }
    if (debug_per_minute) {
        Log.i(TAG, "Here are the points that we have");
        for (int i = 0; i < MAX_POINTS; i++) {
            if (m_points.get(i).rawSensorValue != 0) {
                if (i != m_points.get(i).sensorTime) {
                    Log.i(TAG, "Error in index i = " + i + " sensorTime = " + m_points.get(i).sensorTime);
                }
                // Only print last 60 minutes.
                if (m_libreTrendLatest.id - i < 60) {
                    Log.i(TAG, "" + i + " " + m_points.get(i).rawSensorValue);
                }
            }
        }
    }
    return m_points;
}
Also used : LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock) LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint)

Example 18 with LibreTrendPoint

use of com.eveningoutpost.dexdrip.utils.LibreTrendPoint in project xDrip-plus by jamorham.

the class ReadingData method calculateErrorSet.

// A helper function to calculate the errors and their influence on data.
private static HashSet<Integer> calculateErrorSet(List<LibreTrendPoint> libreTrendPoints, List<GlucoseData> trend) {
    // Create a set of all the points with errors. (without changing libreTrendPoints). Each point with an error
    // has an influence to the next 4 points.
    HashSet<Integer> errorHash = new HashSet<Integer>();
    Iterator<GlucoseData> it = trend.iterator();
    // Find the minimum values to look for error
    int min = libreTrendPoints.size();
    int max = 0;
    while (it.hasNext()) {
        GlucoseData glucoseData = it.next();
        min = Math.min(min, glucoseData.sensorTime);
        max = Math.max(max, glucoseData.sensorTime);
    }
    min = Math.max(0, min - MAX_DISTANCE_FOR_SMOOTHING);
    max = Math.min(libreTrendPoints.size(), max + MAX_DISTANCE_FOR_SMOOTHING);
    for (int i = min; i < max; i++) {
        if (libreTrendPoints.get(i).isError()) {
            for (int j = 0; j < ERROR_INFLUENCE; j++) {
                errorHash.add(i + j);
            }
        }
    }
    return errorHash;
}
Also used : LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint) HashSet(java.util.HashSet)

Example 19 with LibreTrendPoint

use of com.eveningoutpost.dexdrip.utils.LibreTrendPoint in project xDrip-plus by jamorham.

the class ReadingData method calculateSmoothDataPerPoint.

// true means we need to remove this objects.
private boolean calculateSmoothDataPerPoint(GlucoseData glucoseData, List<LibreTrendPoint> libreTrendPoints, HashSet<Integer> errorHash) {
    if (glucoseData.sensorTime < MAX_DISTANCE_FOR_SMOOTHING) {
        // First values are not interesting, but would make the algorithm more complex.
        return false;
    }
    int points_used = 0;
    double sum = 0;
    for (int i = 0; i < MAX_DISTANCE_FOR_SMOOTHING && points_used < PREFERRED_AVERAGE; i++) {
        LibreTrendPoint libreTrendPoint = libreTrendPoints.get(glucoseData.sensorTime - i);
        if (errorHash.contains(glucoseData.sensorTime - i) || libreTrendPoint.rawSensorValue == 0) {
            Log.d(TAG, "Not using point because it is in error" + libreTrendPoint);
            continue;
        }
        sum += libreTrendPoint.rawSensorValue;
        Log.d(TAG, "Using  point for some" + libreTrendPoint);
        points_used++;
    }
    if (points_used > 0) {
        glucoseData.glucoseLevelRawSmoothed = (int) (sum / points_used);
        Log.d(TAG, "setting smooth data based on " + points_used + " points " + glucoseData);
    } else {
        // glucoseData.glucoseLevelRawSmoothed = 0;
        Log.e(TAG, "Removing object because it does not have any data " + glucoseData);
        return true;
    }
    return false;
}
Also used : LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint) LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint)

Example 20 with LibreTrendPoint

use of com.eveningoutpost.dexdrip.utils.LibreTrendPoint in project xDrip by NightscoutFoundation.

the class ReadingDataTest method testCalculateSmoothDataAllValuesTwoErrors.

@Test
public void testCalculateSmoothDataAllValuesTwoErrors() {
    // Having a normal reading data. All data exists (due to further readings).
    // No errors.
    ReadingData readingData = new ReadingData();
    readingData.trend = new ArrayList<GlucoseData>();
    List<LibreTrendPoint> libreTrendPoints = new ArrayList<LibreTrendPoint>(16 * 24 * 60);
    while (libreTrendPoints.size() < 16 * 24 * 60) {
        libreTrendPoints.add(libreTrendPoints.size(), new LibreTrendPoint());
    }
    int[] times = { 0, 2, 4, 6, 7, 12, 15 };
    int[] data = { 200, 220, 240, 260, 270, 320, 350 };
    for (int i = 0; i < times.length; i++) {
        readingData.trend.add(new GlucoseData());
        readingData.trend.get(i).sensorTime = 1000 - times[i];
        readingData.trend.get(i).glucoseLevelRaw = data[i];
        LibreTrendPoint libreTrendPoint = new LibreTrendPoint(1000 - times[i], data[i], 0x700, GlucoseData.DataSource.BLE);
        libreTrendPoints.set((int) libreTrendPoint.getSensorTime(), libreTrendPoint);
    }
    // for LibreTrendPoint add data to fill the gap.
    times = new int[] { 1, 3, 5, 8, 9, 10, 11, 13, 14, 16 };
    data = new int[] { 210, 230, 250, 280, 290, 300, 310, 330, 340, 360 };
    for (int i = 0; i < times.length; i++) {
        LibreTrendPoint libreTrendPoint = new LibreTrendPoint(1000 - times[i], data[i], 0x700, GlucoseData.DataSource.BLE);
        libreTrendPoints.set((int) libreTrendPoint.getSensorTime(), libreTrendPoint);
    }
    // Two errors from 992 to 998
    libreTrendPoints.get(992).rawSensorValue = 0;
    libreTrendPoints.get(995).rawSensorValue = 0;
    for (int i = 0; i < 20; i++) System.err.println(1000 - i + " " + libreTrendPoints.get(1000 - i).rawSensorValue);
    readingData.calculateSmoothDataImproved(libreTrendPoints);
    // Here is how things should look like
    // reading   0       1       2       3   4                   5           6
    // point   1000 999 998 997 996 995 994 993 992 991 990 989 988 987 986 985 984 983
    // raw     200  210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370
    // isError  f    f   t   t   t   t   t   t   t   f   f   f   f   f   f   f   f   f
    // Make sure that one reading data was removed, due to no valid data
    assertWithMessage("One object was removed").that(readingData.trend.size()).isEqualTo(6);
    assertWithMessage("First 2 values should be used").that(readingData.trend.get(0).glucoseLevelRawSmoothed).isEqualTo((200 + 210) / 2);
    assertWithMessage("5 values should be ingnored then 2 used (point 4)").that(readingData.trend.get(1).glucoseLevelRawSmoothed).isEqualTo((290 + 300) / 2);
    assertWithMessage("3 first points ignored, then 4 used values should be used (point 6)").that(readingData.trend.get(2).glucoseLevelRawSmoothed).isEqualTo((290 + 300 + 310 + 320) / 4);
    assertWithMessage("2 first points ignored, then 5 used values should be used (point 7)").that(readingData.trend.get(3).glucoseLevelRawSmoothed).isEqualTo((290 + 300 + 310 + 320 + 330) / 5);
    assertWithMessage("First 5 values should be used (point 12)").that(readingData.trend.get(4).glucoseLevelRawSmoothed).isEqualTo((320 + 330 + 340 + 350 + 360) / 5);
    assertWithMessage("First 5 values should be used (point 15)").that(readingData.trend.get(5).glucoseLevelRawSmoothed).isEqualTo((350 + 360) / 2);
}
Also used : ArrayList(java.util.ArrayList) LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint) LibreTrendPoint(com.eveningoutpost.dexdrip.utils.LibreTrendPoint) Test(org.junit.Test)

Aggregations

LibreTrendPoint (com.eveningoutpost.dexdrip.utils.LibreTrendPoint)30 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 LibreBlock (com.eveningoutpost.dexdrip.Models.LibreBlock)4 LibreTrendUtil (com.eveningoutpost.dexdrip.utils.LibreTrendUtil)2 HashSet (java.util.HashSet)2