Search in sources :

Example 1 with NonMonotonicSequenceException

use of org.apache.commons.math3.exception.NonMonotonicSequenceException in project xDrip-plus by jamorham.

the class LibreAlarmReceiver method CalculateFromDataTransferObject.

public static void CalculateFromDataTransferObject(ReadingData.TransferObject object, boolean use_raw) {
    boolean use_smoothed_data = Pref.getBooleanDefaultFalse("libre_use_smoothed_data");
    // insert any recent data we can
    final List<GlucoseData> mTrend = object.data.trend;
    if (mTrend != null && mTrend.size() > 0) {
        Collections.sort(mTrend);
        final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
        sensorAge = Pref.getInt("nfc_sensor_age", 0);
        if (thisSensorAge > sensorAge || SensorSanity.allowTestingWithDeadSensor()) {
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", false);
            Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
        } else if (thisSensorAge == sensorAge) {
            Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
            JoH.static_toast_long(gs(R.string.sensor_clock_has_not_advanced));
            Pref.setBoolean("nfc_age_problem", true);
            // do not try to insert again
            return;
        } else {
            Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
            JoH.static_toast_long(gs(R.string.sensor_age_has_gone_backwards));
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", true);
        }
        if (d)
            Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
        long shiftx = 0;
        if (mTrend.size() > 0) {
            shiftx = getTimeShift(mTrend);
            if (shiftx != 0)
                Log.d(TAG, "Lag Timeshift: " + shiftx);
            for (GlucoseData gd : mTrend) {
                if (d)
                    Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
                if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
                    if (d)
                        Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
                    continue;
                }
                if (use_raw) {
                    // not quick for recent
                    createBGfromGD(gd, use_smoothed_data, false);
                } else {
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
                }
            }
        } else {
            Log.e(TAG, "Trend data was empty!");
        }
        // munge and insert the history data if any is missing
        final List<GlucoseData> mHistory = object.data.history;
        if ((mHistory != null) && (mHistory.size() > 1)) {
            Collections.sort(mHistory);
            // applyTimeShift(mTrend, shiftx);
            final List<Double> polyxList = new ArrayList<Double>();
            final List<Double> polyyList = new ArrayList<Double>();
            for (GlucoseData gd : mHistory) {
                if (d)
                    Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
                polyxList.add((double) gd.realDate);
                if (use_raw) {
                    polyyList.add((double) gd.glucoseLevelRaw);
                    // For history, data is already averaged, no need for us to use smoothed data
                    createBGfromGD(gd, false, true);
                } else {
                    polyyList.add((double) gd.glucoseLevel);
                    // add in the actual value
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
                }
            }
            // ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
            final SplineInterpolator splineInterp = new SplineInterpolator();
            if (polyxList.size() >= 3) {
                // The need to have at least 3 points is a demand from the interpolate function.
                try {
                    PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
                    final long startTime = mHistory.get(0).realDate;
                    final long endTime = mHistory.get(mHistory.size() - 1).realDate;
                    for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
                        if (d)
                            Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
                        if (use_raw) {
                            // Here we do not use smoothed data, since data is already smoothed for the history
                            createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), false, true);
                        } else {
                            BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
                        }
                    }
                } catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
                    Log.e(TAG, "NonMonotonicSequenceException: " + e);
                }
            }
        } else {
            Log.e(TAG, "no librealarm history data");
        }
    } else {
        Log.d(TAG, "Trend data is null!");
    }
}
Also used : ArrayList(java.util.ArrayList) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Example 2 with NonMonotonicSequenceException

use of org.apache.commons.math3.exception.NonMonotonicSequenceException in project xDrip by NightscoutFoundation.

the class LibreAlarmReceiver method CalculateFromDataTransferObject.

public static void CalculateFromDataTransferObject(ReadingData readingData, boolean use_smoothed_data, boolean use_raw) {
    Log.i(TAG, "CalculateFromDataTransferObject called");
    // insert any recent data we can
    final List<GlucoseData> mTrend = readingData.trend;
    if (mTrend != null && mTrend.size() > 0) {
        Collections.sort(mTrend);
        final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
        sensorAge = Pref.getInt("nfc_sensor_age", 0);
        if (thisSensorAge > sensorAge || SensorSanity.allowTestingWithDeadSensor()) {
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", false);
            Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
        } else if (thisSensorAge == sensorAge) {
            // for a few minutes, this is a problem.
            if (BgReading.getTimeSinceLastReading() > 11 * 60 * 1000) {
                Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
                JoH.static_toast_long(gs(R.string.sensor_clock_has_not_advanced));
                Pref.setBoolean("nfc_age_problem", true);
            }
            // do not try to insert again
            return;
        } else {
            Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
            JoH.static_toast_long(gs(R.string.sensor_age_has_gone_backwards));
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", true);
        }
        if (d)
            Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
        long shiftx = 0;
        if (mTrend.size() > 0) {
            shiftx = getTimeShift(mTrend);
            if (shiftx != 0)
                Log.d(TAG, "Lag Timeshift: " + shiftx);
            for (GlucoseData gd : mTrend) {
                if (d)
                    Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
                if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
                    if (d)
                        Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
                    continue;
                }
                if (use_raw) {
                    // not quick for recent
                    createBGfromGD(gd, use_smoothed_data, false);
                } else {
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
                }
            }
        } else {
            Log.e(TAG, "Trend data was empty!");
        }
        // munge and insert the history data if any is missing
        final List<GlucoseData> mHistory = readingData.history;
        if ((mHistory != null) && (mHistory.size() > 1)) {
            Collections.sort(mHistory);
            // applyTimeShift(mTrend, shiftx);
            final List<Double> polyxList = new ArrayList<Double>();
            final List<Double> polyyList = new ArrayList<Double>();
            for (GlucoseData gd : mHistory) {
                if (d)
                    Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
                polyxList.add((double) gd.realDate);
                if (use_raw) {
                    polyyList.add((double) gd.glucoseLevelRaw);
                    // For history, data is already averaged, no need for us to use smoothed data
                    createBGfromGD(gd, false, true);
                } else {
                    polyyList.add((double) gd.glucoseLevel);
                    // add in the actual value
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
                }
            }
            // ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
            final SplineInterpolator splineInterp = new SplineInterpolator();
            if (polyxList.size() >= 3) {
                // The need to have at least 3 points is a demand from the interpolate function.
                try {
                    PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
                    final long startTime = mHistory.get(0).realDate;
                    final long endTime = mHistory.get(mHistory.size() - 1).realDate;
                    for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
                        if (d)
                            Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
                        if (use_raw) {
                            // Here we do not use smoothed data, since data is already smoothed for the history
                            createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), false, true);
                        } else {
                            BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
                        }
                    }
                } catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
                    Log.e(TAG, "NonMonotonicSequenceException: " + e);
                }
            }
        } else {
            Log.e(TAG, "no librealarm history data");
        }
    } else {
        Log.d(TAG, "Trend data is null!");
    }
}
Also used : ArrayList(java.util.ArrayList) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Example 3 with NonMonotonicSequenceException

use of org.apache.commons.math3.exception.NonMonotonicSequenceException in project xDrip by NightscoutFoundation.

the class LibreAlarmReceiver method CalculateFromDataTransferObject.

public static void CalculateFromDataTransferObject(ReadingData.TransferObject object, boolean use_raw) {
    boolean use_smoothed_data = Pref.getBooleanDefaultFalse("libre_use_smoothed_data");
    // insert any recent data we can
    final List<GlucoseData> mTrend = object.data.trend;
    if (mTrend != null && mTrend.size() > 0) {
        Collections.sort(mTrend);
        final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
        sensorAge = Pref.getInt("nfc_sensor_age", 0);
        if (thisSensorAge > sensorAge || SensorSanity.allowTestingWithDeadSensor()) {
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", false);
            Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
        } else if (thisSensorAge == sensorAge) {
            Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
            JoH.static_toast_long(gs(R.string.sensor_clock_has_not_advanced));
            Pref.setBoolean("nfc_age_problem", true);
            // do not try to insert again
            return;
        } else {
            Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
            JoH.static_toast_long(gs(R.string.sensor_age_has_gone_backwards));
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", true);
        }
        if (d)
            Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
        long shiftx = 0;
        if (mTrend.size() > 0) {
            shiftx = getTimeShift(mTrend);
            if (shiftx != 0)
                Log.d(TAG, "Lag Timeshift: " + shiftx);
            for (GlucoseData gd : mTrend) {
                if (d)
                    Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
                if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
                    if (d)
                        Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
                    continue;
                }
                if (use_raw) {
                    // not quick for recent
                    createBGfromGD(gd, use_smoothed_data, false);
                } else {
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
                }
            }
        } else {
            Log.e(TAG, "Trend data was empty!");
        }
        // munge and insert the history data if any is missing
        final List<GlucoseData> mHistory = object.data.history;
        if ((mHistory != null) && (mHistory.size() > 1)) {
            Collections.sort(mHistory);
            // applyTimeShift(mTrend, shiftx);
            final List<Double> polyxList = new ArrayList<Double>();
            final List<Double> polyyList = new ArrayList<Double>();
            for (GlucoseData gd : mHistory) {
                if (d)
                    Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
                polyxList.add((double) gd.realDate);
                if (use_raw) {
                    polyyList.add((double) gd.glucoseLevelRaw);
                    // For history, data is already averaged, no need for us to use smoothed data
                    createBGfromGD(gd, false, true);
                } else {
                    polyyList.add((double) gd.glucoseLevel);
                    // add in the actual value
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
                }
            }
            // ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
            final SplineInterpolator splineInterp = new SplineInterpolator();
            if (polyxList.size() >= 3) {
                // The need to have at least 3 points is a demand from the interpolate function.
                try {
                    PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
                    final long startTime = mHistory.get(0).realDate;
                    final long endTime = mHistory.get(mHistory.size() - 1).realDate;
                    for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
                        if (d)
                            Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
                        if (use_raw) {
                            // Here we do not use smoothed data, since data is already smoothed for the history
                            createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), false, true);
                        } else {
                            BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
                        }
                    }
                } catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
                    Log.e(TAG, "NonMonotonicSequenceException: " + e);
                }
            }
        } else {
            Log.e(TAG, "no librealarm history data");
        }
    } else {
        Log.d(TAG, "Trend data is null!");
    }
}
Also used : ArrayList(java.util.ArrayList) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Example 4 with NonMonotonicSequenceException

use of org.apache.commons.math3.exception.NonMonotonicSequenceException in project xDrip-plus by jamorham.

the class LibreAlarmReceiver method CalculateFromDataTransferObject.

public static void CalculateFromDataTransferObject(ReadingData readingData, boolean use_smoothed_data, boolean use_raw) {
    Log.i(TAG, "CalculateFromDataTransferObject called");
    // insert any recent data we can
    final List<GlucoseData> mTrend = readingData.trend;
    if (mTrend != null && mTrend.size() > 0) {
        Collections.sort(mTrend);
        final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
        sensorAge = Pref.getInt("nfc_sensor_age", 0);
        if (thisSensorAge > sensorAge || SensorSanity.allowTestingWithDeadSensor()) {
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", false);
            Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
        } else if (thisSensorAge == sensorAge) {
            // for a few minutes, this is a problem.
            if (BgReading.getTimeSinceLastReading() > 11 * 60 * 1000) {
                Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
                JoH.static_toast_long(gs(R.string.sensor_clock_has_not_advanced));
                Pref.setBoolean("nfc_age_problem", true);
            }
            // do not try to insert again
            return;
        } else {
            Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
            JoH.static_toast_long(gs(R.string.sensor_age_has_gone_backwards));
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", true);
        }
        if (d)
            Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
        long shiftx = 0;
        if (mTrend.size() > 0) {
            shiftx = getTimeShift(mTrend);
            if (shiftx != 0)
                Log.d(TAG, "Lag Timeshift: " + shiftx);
            for (GlucoseData gd : mTrend) {
                if (d)
                    Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
                if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
                    if (d)
                        Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
                    continue;
                }
                if (use_raw) {
                    // not quick for recent
                    createBGfromGD(gd, use_smoothed_data, false);
                } else {
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
                }
            }
        } else {
            Log.e(TAG, "Trend data was empty!");
        }
        // munge and insert the history data if any is missing
        final List<GlucoseData> mHistory = readingData.history;
        if ((mHistory != null) && (mHistory.size() > 1)) {
            Collections.sort(mHistory);
            // applyTimeShift(mTrend, shiftx);
            final List<Double> polyxList = new ArrayList<Double>();
            final List<Double> polyyList = new ArrayList<Double>();
            for (GlucoseData gd : mHistory) {
                if (d)
                    Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
                polyxList.add((double) gd.realDate);
                if (use_raw) {
                    polyyList.add((double) gd.glucoseLevelRaw);
                    // For history, data is already averaged, no need for us to use smoothed data
                    createBGfromGD(gd, false, true);
                } else {
                    polyyList.add((double) gd.glucoseLevel);
                    // add in the actual value
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
                }
            }
            // ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
            final SplineInterpolator splineInterp = new SplineInterpolator();
            if (polyxList.size() >= 3) {
                // The need to have at least 3 points is a demand from the interpolate function.
                try {
                    PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
                    final long startTime = mHistory.get(0).realDate;
                    final long endTime = mHistory.get(mHistory.size() - 1).realDate;
                    for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
                        if (d)
                            Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
                        if (use_raw) {
                            // Here we do not use smoothed data, since data is already smoothed for the history
                            createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), false, true);
                        } else {
                            BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
                        }
                    }
                } catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
                    Log.e(TAG, "NonMonotonicSequenceException: " + e);
                }
            }
        } else {
            Log.e(TAG, "no librealarm history data");
        }
    } else {
        Log.d(TAG, "Trend data is null!");
    }
}
Also used : ArrayList(java.util.ArrayList) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Example 5 with NonMonotonicSequenceException

use of org.apache.commons.math3.exception.NonMonotonicSequenceException in project mafscaling by vimsh.

the class BilinearInterpolator method interpolate.

@Override
public BivariateFunction interpolate(double[] xval, double[] yval, double[][] fval) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException, NumberIsTooSmallException {
    if (xval.length == 0 || yval.length == 0 || fval.length == 0)
        throw new NoDataException();
    if (xval.length != fval.length)
        throw new DimensionMismatchException(xval.length, fval.length);
    if (yval.length != fval[0].length)
        throw new DimensionMismatchException(yval.length, fval[0].length);
    MathArrays.checkOrder(xval);
    MathArrays.checkOrder(yval);
    return new BilinearInterpolatingFunction(xval, yval, fval);
}
Also used : DimensionMismatchException(org.apache.commons.math3.exception.DimensionMismatchException) NoDataException(org.apache.commons.math3.exception.NoDataException)

Aggregations

GlucoseData (com.eveningoutpost.dexdrip.Models.GlucoseData)4 ArrayList (java.util.ArrayList)4 SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)4 PolynomialSplineFunction (org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction)4 DimensionMismatchException (org.apache.commons.math3.exception.DimensionMismatchException)1 NoDataException (org.apache.commons.math3.exception.NoDataException)1