Example usage for com.google.common.primitives Doubles indexOf

List of usage examples for com.google.common.primitives Doubles indexOf

Introduction

In this page you can find the example usage for com.google.common.primitives Doubles indexOf.

Prototype

public static int indexOf(double[] array, double[] target) 

Source Link

Document

Returns the start position of the first occurrence of the specified target within array , or -1 if there is no such occurrence.

Usage

From source file:controller.ClassificationController.java

public static void classify(String classifier, FeatureModel featureModel) {

    //scaling of features
    float[][] features_scaled = Util.deepcopy(featureModel.getFeatures());
    double[][] scaling = (double[][]) Util.load(classifier.replace("[model]", "[scaling]"));

    double[] mean = scaling[0];
    double[] std = scaling[1];
    for (float[] feature : features_scaled) {
        for (int j = 0; j < feature.length; j++) {
            feature[j] = (float) ((feature[j] - mean[j]) / std[j]);
        }//w w w. j  av a  2 s  . c om
    }

    //load classifier 1 for arousals
    ClassificationController svmArousal = new ClassificationController();
    svmArousal.svmLoadModel(classifier.replace("[model]", "[arousal]"));

    //load classifier 2 for sleep stages
    ClassificationController svm = new ClassificationController();
    svm.svmLoadModel(classifier);

    //load indices indicating the features of the feature vector of classifier 2
    String dir = System.getProperty("user.dir");
    String filename = "sparse_features.jo";
    String fullPath = dir + File.separator + "Classifiers" + File.separator + filename;
    double[] fidx = (double[]) Util.load(fullPath);

    float[] kcPercentage = featureModel.getKcPercentage();

    double[] output;

    int N1flag = 0;
    int Wflag = 0;

    for (int i = 0; i < featureModel.getNumberOfEpochs(); i++) {
        //get unscaled features
        float[] features_unscaled = featureModel.getFeatureVector(i);

        //use only subset of feature vector as stored in sparse_features.jo array
        float[] f1 = new float[fidx.length];
        for (int j = 0; j < f1.length; j++) {
            //use scaled features here
            f1[j] = features_scaled[i][(int) (fidx[j] - 1)];
        }

        output = svm.svmPredict(Util.floatToDouble(f1));
        int classLabel = Doubles.indexOf(output, Doubles.max(output));

        double W = features_unscaled[18];
        double N2 = features_unscaled[1]; //(skewness)
        double N3 = features_unscaled[49]; //cepstrum
        double MA1 = features_unscaled[3]; //max value of high passed epoch
        double MA2 = features_unscaled[9]; //energy in HF wavelet band
        double KC = kcPercentage[i];

        //hand selected features for arousals
        boolean MA3 = MA1 > 40 & MA2 > 0.4;

        double[] MAtmp = svmArousal.svmPredict(Util.floatToDouble(features_scaled[i]));
        boolean MA = (MAtmp[1] > MAtmp[0]) | MA3;

        //convert SVM labels to SleepPilot labels (4 is REM)
        if (classLabel == 4) {
            classLabel = 5;
        }

        if (classLabel == 5 & MA) {
            classLabel = 1;
        }

        if (classLabel == 2 & KC == 0 & MA) {
            classLabel = 1;
        }

        //adjust classifier output to be consistent with SleepPilots K-complex detector
        if (classLabel == 3 & KC <= 20) {
            classLabel = 2;
        }
        if (classLabel == 2 & KC > 20 & !MA) {
            classLabel = 3;
        }

        //set artefacts and arousals to zero (standard)
        featureModel.setArtefact(i, 0);
        featureModel.setArousal(i, 0);

        if (classLabel != 0 & MA) {
            featureModel.setArtefact(i, 1);
            featureModel.setArousal(i, 1);
        }
        if (classLabel != 0 & MA1 > 80) {
            featureModel.setArtefact(i, 1);
        }

        //REM is always after N3, at least in non-pathological cases
        if (classLabel == 3) {
            N1flag += 1;
        }

        if (classLabel == 5 & N1flag < 2) {
            classLabel = 1;
        }

        featureModel.setPredictProbabilities(i, output.clone());
        featureModel.setLabel(i, classLabel);
        System.out.println("Predicted Class Label: " + classLabel);

    }

    featureModel.setClassificationDone(true);
}

From source file:de.msal.shoutemo.connector.Connection.java

/**
 * <strong>Tries to correctly set the users timezone on autemo.com.</strong>
 * <p/>/*from   w ww  .j  a  va2 s  .c om*/
 * Since autemos online timezone setting is plain wrong try to take care of it here. We have to
 * subtract 1h of the real offset. Two problems, with that: <ol><li> There are timezones with
 * half hours, that can't always be reduced by one. e.g. UTC+9.5h can't go down to UTC+8.5, as
 * such a timezon doesn't exist. In that case we just reduce it by 1.5h, to UTC+8. This way
 * messages display 30min older than they are, but this is still better than them being
 * displayed 30min in the future. </li><li> Can't decrease UTC-12 as it is the international
 * date line. Just ignore that case, however, as only two uninhabited islands (Baker Island and
 * Howland Island) use UTC-12. </li></ol>
 *
 * @param authtoken     this sessions authtoken.
 * @param offsetInHours the real offset of the desired timezone, <strong>including
 *                      <i>possible</i> daylight savings time</strong>, in hours!
 * @return the http status code of the response. If the provided {@code offsetInHours} couldn't
 * be mapped to a timezone value, {@code -1} is returned.
 */
public static int setUserTimezone(String authtoken, double offsetInHours) throws IOException {

    double[] timezoneMapping = new double[35];
    timezoneMapping[0] = -12; // value="1"    GMT/UTC -12:00 hours
    timezoneMapping[1] = -11; // value="2"    GMT/UTC -11:00 hours
    timezoneMapping[2] = -10; // value="3"    GMT/UTC -10:00 hours
    timezoneMapping[3] = -9.5; // value="4"    GMT/UTC  -9:30 hours
    timezoneMapping[4] = -9; // value="5"    GMT/UTC  -9:00 hours
    timezoneMapping[5] = -8.5; // value="6"    GMT/UTC  -8:30 hours
    timezoneMapping[6] = -8; // value="7"    GMT/UTC  -8:00 hours
    timezoneMapping[7] = -7; // value="8"    GMT/UTC  -7:00 hours
    timezoneMapping[8] = -6; // value="9"    GMT/UTC  -6:00 hours
    timezoneMapping[9] = -5; // value="10"   GMT/UTC  -5:00 hours
    timezoneMapping[10] = -4; // value="11"   GMT/UTC  -4:00 hours
    timezoneMapping[11] = -3.5; // value="12"   GMT/UTC  -3:30 hours
    timezoneMapping[12] = -3; // value="13"   GMT/UTC  -3:00 hours
    timezoneMapping[13] = -2; // value="14"   GMT/UTC  -2:00 hours
    timezoneMapping[14] = -1; // value="15"   GMT/UTC  -1:00 hours
    timezoneMapping[15] = 0; // value="16"   GMT/UTC
    timezoneMapping[16] = 1; // value="17"   GMT/UTC  +1:00 hours
    timezoneMapping[17] = 2; // value="18"   GMT/UTC  +2:00 hours
    timezoneMapping[18] = 3; // value="19"   GMT/UTC  +3:00 hours
    timezoneMapping[19] = 4; // value="20"   GMT/UTC  +4:00 hours
    timezoneMapping[20] = 5; // value="21"   GMT/UTC  +5:00 hours
    timezoneMapping[21] = 5.5; // value="22"   GMT/UTC  +5:30 hours
    timezoneMapping[22] = 6; // value="23"   GMT/UTC  +6:00 hours
    timezoneMapping[23] = 6.5; // value="24"   GMT/UTC  +6:30 hours
    timezoneMapping[24] = 7; // value="25"   GMT/UTC  +7:00 hours
    timezoneMapping[25] = 8; // value="26"   GMT/UTC  +8:00 hours
    timezoneMapping[26] = 9; // value="27"   GMT/UTC  +9:00 hours
    timezoneMapping[27] = 9.5; // value="28"   GMT/UTC  +9:30 hours
    timezoneMapping[28] = 10; // value="29"   GMT/UTC +10:00 hours
    timezoneMapping[29] = 10.5; // value="30"   GMT/UTC +10:30 hours
    timezoneMapping[30] = 11; // value="31"   GMT/UTC +11:00 hours
    timezoneMapping[31] = 11.5; // value="32"   GMT/UTC +11:30 hours
    timezoneMapping[32] = 12; // value="33"   GMT/UTC +12:00 hours
    timezoneMapping[33] = 13; // value="34"   GMT/UTC +13:00 hours
    timezoneMapping[34] = 14; // value="35"   GMT/UTC +14:00 hours

    /*
     * Since autemos online timezone setting is plain wrong try to take care of it here. We have
     * to subtract 1h of the real offset. Two problems, with that:
     *   1. There are timezones with half hours, that can't always be reduced by one.
     *      e.g. UTC+9.5h can't go down to UTC+8.5, as such a timezon doesn't exist. In that
     *      case we just reduce it by 1.5h, to UTC+8. This way messages display 30min older than
     *      they are, but this is still better than them being displayed 30min in the future.
     *   2. Can't decrease UTC-12 as it is the international date line. Just ignore that case,
     *      however, as only two uninhabited islands (Baker Island and Howland Island) use
     *      UTC-12.
     */
    double autemoOffsetInHours = Doubles.indexOf(timezoneMapping, offsetInHours - 1) == -1 ? offsetInHours - 1.5
            : offsetInHours - 1;
    // find the mapped code (== index)
    int offsetCode = Doubles.indexOf(timezoneMapping, autemoOffsetInHours);

    if (offsetCode > 0) {
        /* first get necessary data */
        Document doc = Jsoup.connect("http://www.autemo.com/myaccount/?m=settings").timeout(TIMEOUT)
                .ignoreHttpErrors(true).followRedirects(true).cookie("PHPSESSID", authtoken)
                .userAgent(USER_AGENT).get();

        String firstName = doc.getElementsByAttributeValue("name", "x_firstname").val();
        String lastName = doc.getElementsByAttributeValue("name", "x_lastname").val();
        String email = doc.getElementsByAttributeValue("name", "x_email").val();
        String countryid = doc.getElementsByAttributeValue("name", "x_countryid").get(0)
                .getElementsByAttributeValue("selected", "selected").val();

        /* now update the timezone */
        int statusCode = Jsoup.connect("http://www.autemo.com/myaccount/?m=settings").timeout(TIMEOUT)
                .ignoreHttpErrors(true).followRedirects(true).cookie("PHPSESSID", authtoken)
                .userAgent(USER_AGENT).data("x_timezoneid", String.valueOf(offsetCode))
                .data("submitted", "true").data("x_countryid", countryid).data("x_email", email)
                .data("x_firstname", firstName).data("x_lastname", lastName)
                .method(org.jsoup.Connection.Method.POST).execute().statusCode();
        Log.v(TAG,
                "Changing user timezone to " + offsetCode + " (timezone code) (GMT+" + offsetInHours
                        + " (offsetInHours); autemoOffsetInHours=" + autemoOffsetInHours + "). Server answer="
                        + statusCode);
        return statusCode;
    } else {
        return -1;
    }
}