Example usage for org.apache.commons.math3.linear RealVector setEntry

List of usage examples for org.apache.commons.math3.linear RealVector setEntry

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector setEntry.

Prototype

public abstract void setEntry(int index, double value) throws OutOfRangeException;

Source Link

Document

Set a single element.

Usage

From source file:edu.stanford.cfuller.imageanalysistools.fitting.GaussianImageObject.java

/**
 * Fits this object to a 3-dimensional gaussian, and estimates error and goodness of fit.
 * @param p     The parameters for the current analysis.
 *//*  w  ww.  j ava 2 s. c  o m*/
public void fitPosition(ParameterDictionary p) {

    if (this.sizeInPixels == 0) {
        this.nullifyImages();
        return;
    }

    this.fitParametersByChannel = new java.util.ArrayList<FitParameters>();
    this.fitR2ByChannel = new java.util.ArrayList<Double>();
    this.fitErrorByChannel = new java.util.ArrayList<Double>();
    this.nPhotonsByChannel = new java.util.ArrayList<Double>();

    GaussianFitter3D gf = new GaussianFitter3D();

    //System.out.println(this.parent.getDimensionSizes().getZ());

    int numChannels = 0;

    if (p.hasKey(NUM_WAVELENGTHS_PARAM)) {
        numChannels = p.getIntValueForKey(NUM_WAVELENGTHS_PARAM);
    } else {
        numChannels = this.parent.getDimensionSizes().get(ImageCoordinate.C);
    }

    for (int channelIndex = 0; channelIndex < numChannels; channelIndex++) {

        RealVector fitParameters = new ArrayRealVector(7, 0.0);

        double ppg = p.getDoubleValueForKey(PHOTONS_PER_LEVEL_PARAM);

        this.parentBoxMin.set(ImageCoordinate.C, channelIndex);
        this.parentBoxMax.set(ImageCoordinate.C, channelIndex + 1);

        this.boxImages();

        List<Double> x = new java.util.ArrayList<Double>();
        List<Double> y = new java.util.ArrayList<Double>();
        List<Double> z = new java.util.ArrayList<Double>();
        List<Double> f = new java.util.ArrayList<Double>();

        for (ImageCoordinate ic : this.parent) {
            x.add((double) ic.get(ImageCoordinate.X));
            y.add((double) ic.get(ImageCoordinate.Y));
            z.add((double) ic.get(ImageCoordinate.Z));
            f.add((double) parent.getValue(ic));
        }

        xValues = new double[x.size()];
        yValues = new double[y.size()];
        zValues = new double[z.size()];
        functionValues = new double[f.size()];

        double xCentroid = 0;
        double yCentroid = 0;
        double zCentroid = 0;
        double totalCounts = 0;

        for (int i = 0; i < x.size(); i++) {

            xValues[i] = x.get(i);
            yValues[i] = y.get(i);
            zValues[i] = z.get(i);
            functionValues[i] = f.get(i) * ppg;
            xCentroid += xValues[i] * functionValues[i];
            yCentroid += yValues[i] * functionValues[i];
            zCentroid += zValues[i] * functionValues[i];
            totalCounts += functionValues[i];
        }

        xCentroid /= totalCounts;
        yCentroid /= totalCounts;
        zCentroid /= totalCounts;

        //z sometimes seems to be a bit off... trying (20110415) to go back to max value pixel at x,y centroid

        int xRound = (int) Math.round(xCentroid);
        int yRound = (int) Math.round(yCentroid);

        double maxVal = 0;
        int maxInd = 0;

        double minZ = Double.MAX_VALUE;
        double maxZ = 0;

        for (int i = 0; i < x.size(); i++) {

            if (zValues[i] < minZ)
                minZ = zValues[i];
            if (zValues[i] > maxZ)
                maxZ = zValues[i];

            if (xValues[i] == xRound && yValues[i] == yRound) {
                if (functionValues[i] > maxVal) {
                    maxVal = functionValues[i];
                    maxInd = (int) zValues[i];
                }
            }
        }

        zCentroid = maxInd;

        //parameter ordering: amplitude, var x-y, var z, x/y/z coords, background

        //amplitude: find the max value; background: find the min value

        double maxValue = 0;

        double minValue = Double.MAX_VALUE;

        for (ImageCoordinate ic : this.parent) {

            if (parent.getValue(ic) > maxValue)
                maxValue = parent.getValue(ic);
            if (parent.getValue(ic) < minValue)
                minValue = parent.getValue(ic);

        }

        fitParameters.setEntry(0, (maxValue - minValue) * 0.95);
        fitParameters.setEntry(6, minValue + 0.05 * (maxValue - minValue));

        //positions

        fitParameters.setEntry(3, xCentroid);
        fitParameters.setEntry(4, yCentroid);
        fitParameters.setEntry(5, zCentroid);

        //variances

        final double limitedWidthxy = 200;
        final double limitedWidthz = 500;

        double sizex = limitedWidthxy / p.getDoubleValueForKey(PIXELSIZE_PARAM);
        double sizez = limitedWidthz / p.getDoubleValueForKey(SECTIONSIZE_PARAM);

        if (p.hasKey(Z_WIDTH_PARAM)) {
            sizez = p.getDoubleValueForKey(Z_WIDTH_PARAM);
        }

        if (p.hasKey(XY_WIDTH_PARAM)) {
            sizex = p.getDoubleValueForKey(XY_WIDTH_PARAM);
        }

        fitParameters.setEntry(1, sizex / 2);
        fitParameters.setEntry(2, sizez / 2);

        //amplitude and background are in arbitrary intensity units; convert to photon counts

        fitParameters.setEntry(0, fitParameters.getEntry(0) * ppg);
        fitParameters.setEntry(6, fitParameters.getEntry(6) * ppg);

        //System.out.println("guess: " + fitParameters);

        //do the fit

        fitParameters = gf.fit(this, fitParameters, ppg);

        //System.out.println("fit: " + fitParameters);

        FitParameters fp = new FitParameters();

        fp.setPosition(ImageCoordinate.X, fitParameters.getEntry(3));
        fp.setPosition(ImageCoordinate.Y, fitParameters.getEntry(4));
        fp.setPosition(ImageCoordinate.Z, fitParameters.getEntry(5));

        fp.setSize(ImageCoordinate.X, fitParameters.getEntry(1));
        fp.setSize(ImageCoordinate.Y, fitParameters.getEntry(1));
        fp.setSize(ImageCoordinate.Z, fitParameters.getEntry(2));

        fp.setAmplitude(fitParameters.getEntry(0));
        fp.setBackground(fitParameters.getEntry(6));

        fitParametersByChannel.add(fp);

        //calculate R2

        double residualSumSquared = 0;
        double mean = 0;
        double variance = 0;
        double R2 = 0;

        double n_photons = 0;

        for (int i = 0; i < this.xValues.length; i++) {

            residualSumSquared += Math.pow(GaussianFitter3D.fitResidual(functionValues[i], xValues[i],
                    yValues[i], zValues[i], fitParameters), 2);

            mean += functionValues[i];

            n_photons += functionValues[i] - fitParameters.getEntry(6);

        }

        mean /= functionValues.length;

        for (int i = 0; i < this.xValues.length; i++) {
            variance += Math.pow(functionValues[i] - mean, 2);
        }

        R2 = 1 - (residualSumSquared / variance);

        this.fitR2ByChannel.add(R2);

        this.unboxImages();

        //calculate fit error

        double s_xy = fitParameters.getEntry(1) * fitParameters.getEntry(1)
                * Math.pow(p.getDoubleValueForKey(PIXELSIZE_PARAM), 2);
        double s_z = fitParameters.getEntry(2) * fitParameters.getEntry(2)
                * Math.pow(p.getDoubleValueForKey(SECTIONSIZE_PARAM), 2);

        //s_z = 0; //remove!!

        double error = (2 * s_xy + s_z) / (n_photons - 1);// + 4*Math.sqrt(Math.PI) * Math.pow(2*s_xy,1.5)*Math.pow(fitParameters.getEntry(6),2)/(p.getDoubleValueForKey("pixelsize_nm")*n_photons*n_photons);

        double b = fitParameters.getEntry(6);
        double a = p.getDoubleValueForKey(PIXELSIZE_PARAM);
        double alpha = p.getDoubleValueForKey(SECTIONSIZE_PARAM);
        double sa_x = s_xy + Math.pow(a, 2) / 12;
        double sa_z = s_z + Math.pow(alpha, 2) / 12;

        //System.out.printf("b = %f, a = %f, alpha = %f, s_xy = %f, s_z = %f, n= %f\n", b, a, alpha, s_xy, s_z, n_photons);

        double error_x = sa_x / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_x * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey(PIXELSIZE_PARAM), 2)));
        double error_z = sa_z / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_z * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey(SECTIONSIZE_PARAM), 2)));

        double A = 1.0 / (2 * Math.sqrt(2) * Math.pow(Math.PI, 1.5) * Math.sqrt(sa_z) * sa_x);

        ErrIntFunc eif = new ErrIntFunc();

        eif.setParams(b, n_photons, A, sa_z, sa_x, a, alpha);

        LegendreGaussIntegrator lgi = new LegendreGaussIntegrator(5, 10, 1000);

        //integrate over 10*width of PSF in z 

        double size = 10 * Math.sqrt(sa_z);

        double intpart = 0;
        try {

            if (b < 0)
                throw new ConvergenceException(new DummyLocalizable("negative background!")); // a negative value for b seems to cause the integration to hang, preventing the program from progressing

            intpart = lgi.integrate(10000, eif, -size, size);

            double fullIntPart = intpart + Math.pow(2 * Math.PI, 1.5) * sa_x * A / Math.sqrt(sa_z);

            error_x = Math.sqrt(2 / (n_photons * sa_z / (2 * sa_z + sa_x) * fullIntPart));
            error_z = Math.sqrt(2 / (n_photons * sa_x / (2 * sa_z + sa_x) * fullIntPart));

        } catch (ConvergenceException e) {
            LoggingUtilities.getLogger().severe("Integration error: " + e.getMessage());
            error_x = -1;
            error_z = -1;
        }

        if (error_x > 0 && error_z > 0) {

            error = Math.sqrt(2 * error_x * error_x + error_z * error_z);

        } else {
            error = Double.NaN;
        }

        this.fitErrorByChannel.add(error);

        this.positionsByChannel.add(fitParameters.getSubVector(3, 3));

        this.nPhotonsByChannel.add(n_photons);

    }

    this.hadFittingError = false;
}

From source file:edu.stanford.cfuller.imageanalysistools.fitting.GaussianImageObjectWithCovariance.java

/**
 * Fits this object to a 3-dimensional gaussian, and estimates error and goodness of fit.
 * @param p     The parameters for the current analysis.
 *//*from www  .  j  ava2 s.  c  o  m*/
public void fitPosition(ParameterDictionary p) {

    if (this.sizeInPixels == 0) {
        this.nullifyImages();
        return;
    }

    this.fitParametersByChannel = new java.util.ArrayList<FitParameters>();
    this.fitR2ByChannel = new java.util.ArrayList<Double>();
    this.fitErrorByChannel = new java.util.ArrayList<Double>();
    this.nPhotonsByChannel = new java.util.ArrayList<Double>();

    GaussianFitter3DWithCovariance gf = new GaussianFitter3DWithCovariance();

    //System.out.println(this.parent.getDimensionSizes().getZ());

    int numChannels = 0;

    if (p.hasKey("num_wavelengths")) {
        numChannels = p.getIntValueForKey("num_wavelengths");
    } else {
        numChannels = this.parent.getDimensionSizes().get(ImageCoordinate.C);
    }

    //for (int channelIndex = 0; channelIndex < this.parent.getDimensionSizes().getC(); channelIndex++) {
    for (int channelIndex = 0; channelIndex < numChannels; channelIndex++) {

        RealVector fitParameters = new ArrayRealVector(11, 0.0);

        double ppg = p.getDoubleValueForKey("photons_per_greylevel");

        this.parentBoxMin.set(ImageCoordinate.C, channelIndex);
        this.parentBoxMax.set(ImageCoordinate.C, channelIndex + 1);

        this.boxImages();

        List<Double> x = new java.util.ArrayList<Double>();
        List<Double> y = new java.util.ArrayList<Double>();
        List<Double> z = new java.util.ArrayList<Double>();
        List<Double> f = new java.util.ArrayList<Double>();

        for (ImageCoordinate ic : this.parent) {
            x.add((double) ic.get(ImageCoordinate.X));
            y.add((double) ic.get(ImageCoordinate.Y));
            z.add((double) ic.get(ImageCoordinate.Z));
            f.add((double) parent.getValue(ic));
        }

        xValues = new double[x.size()];
        yValues = new double[y.size()];
        zValues = new double[z.size()];
        functionValues = new double[f.size()];

        double xCentroid = 0;
        double yCentroid = 0;
        double zCentroid = 0;
        double totalCounts = 0;

        for (int i = 0; i < x.size(); i++) {

            xValues[i] = x.get(i);
            yValues[i] = y.get(i);
            zValues[i] = z.get(i);
            functionValues[i] = f.get(i) * ppg;
            xCentroid += xValues[i] * functionValues[i];
            yCentroid += yValues[i] * functionValues[i];
            zCentroid += zValues[i] * functionValues[i];
            totalCounts += functionValues[i];
        }

        xCentroid /= totalCounts;
        yCentroid /= totalCounts;
        zCentroid /= totalCounts;

        //z sometimes seems to be a bit off... trying (20110415) to go back to max value pixel at x,y centroid

        int xRound = (int) Math.round(xCentroid);
        int yRound = (int) Math.round(yCentroid);

        double maxVal = 0;
        int maxInd = 0;

        double minZ = Double.MAX_VALUE;
        double maxZ = 0;

        for (int i = 0; i < x.size(); i++) {

            if (zValues[i] < minZ)
                minZ = zValues[i];
            if (zValues[i] > maxZ)
                maxZ = zValues[i];

            if (xValues[i] == xRound && yValues[i] == yRound) {
                if (functionValues[i] > maxVal) {
                    maxVal = functionValues[i];
                    maxInd = (int) zValues[i];
                }
            }
        }

        zCentroid = maxInd;

        //parameter ordering: amplitude, var x-y, var z, x/y/z coords, background

        //amplitude: find the max value; background: find the min value

        double maxValue = 0;

        double minValue = Double.MAX_VALUE;

        for (ImageCoordinate ic : this.parent) {

            if (parent.getValue(ic) > maxValue)
                maxValue = parent.getValue(ic);
            if (parent.getValue(ic) < minValue)
                minValue = parent.getValue(ic);

        }

        fitParameters.setEntry(0, (maxValue - minValue) * 0.95);
        fitParameters.setEntry(10, minValue + 0.05 * (maxValue - minValue));

        //positions

        fitParameters.setEntry(7, xCentroid);
        fitParameters.setEntry(8, yCentroid);
        fitParameters.setEntry(9, zCentroid);

        //variances

        final double limitedWidthxy = 200;
        final double limitedWidthz = 500;

        double sizex = limitedWidthxy / p.getDoubleValueForKey("pixelsize_nm");
        double sizey = limitedWidthxy / p.getDoubleValueForKey("pixelsize_nm");
        double sizez = limitedWidthz / p.getDoubleValueForKey("z_sectionsize_nm");

        if (p.hasKey(Z_WIDTH_PARAM)) {
            sizez = p.getDoubleValueForKey(Z_WIDTH_PARAM);
        }

        if (p.hasKey(XY_WIDTH_PARAM)) {
            sizex = p.getDoubleValueForKey(XY_WIDTH_PARAM);
            sizey = p.getDoubleValueForKey(XY_WIDTH_PARAM);
        }

        fitParameters.setEntry(1, sizex / 2);
        fitParameters.setEntry(2, sizey / 2);
        fitParameters.setEntry(3, sizez / 2);

        //covariances -- guess zero to start

        fitParameters.setEntry(4, 0.0);
        fitParameters.setEntry(5, 0.0);
        fitParameters.setEntry(6, 0.0);

        //amplitude and background are in arbitrary intensity units; convert to photon counts

        fitParameters.setEntry(0, fitParameters.getEntry(0) * ppg);
        fitParameters.setEntry(10, fitParameters.getEntry(10) * ppg);

        //System.out.println("guess: " + fitParameters);

        //do the fit

        //System.out.println("Initial for object " + this.label + ": " + fitParameters.toString());

        fitParameters = gf.fit(this, fitParameters, ppg);

        //System.out.println("Parameters for object " + this.label + ": " + fitParameters.toString());

        //System.out.println("fit: " + fitParameters);

        FitParameters fp = new FitParameters();

        fp.setPosition(ImageCoordinate.X, fitParameters.getEntry(7));
        fp.setPosition(ImageCoordinate.Y, fitParameters.getEntry(8));
        fp.setPosition(ImageCoordinate.Z, fitParameters.getEntry(9));

        fp.setSize(ImageCoordinate.X, fitParameters.getEntry(1));
        fp.setSize(ImageCoordinate.Y, fitParameters.getEntry(2));
        fp.setSize(ImageCoordinate.Z, fitParameters.getEntry(3));

        fp.setAmplitude(fitParameters.getEntry(0));
        fp.setBackground(fitParameters.getEntry(10));

        fp.setOtherParameter("corr_xy", fitParameters.getEntry(4));
        fp.setOtherParameter("corr_xz", fitParameters.getEntry(5));
        fp.setOtherParameter("corr_yz", fitParameters.getEntry(6));

        fitParametersByChannel.add(fp);

        //            System.out.println(fitParameters);

        //calculate R2

        double residualSumSquared = 0;
        double mean = 0;
        double variance = 0;
        double R2 = 0;

        double n_photons = 0;

        for (int i = 0; i < this.xValues.length; i++) {

            residualSumSquared += Math.pow(GaussianFitter3DWithCovariance.fitResidual(functionValues[i],
                    xValues[i], yValues[i], zValues[i], fitParameters), 2);

            mean += functionValues[i];

            n_photons += functionValues[i] - fitParameters.getEntry(10);

        }

        mean /= functionValues.length;

        for (int i = 0; i < this.xValues.length; i++) {
            variance += Math.pow(functionValues[i] - mean, 2);
        }

        R2 = 1 - (residualSumSquared / variance);

        this.fitR2ByChannel.add(R2);

        this.unboxImages();

        //calculate fit error -- these are wrong for the case with unequal x-y variances and covariance allowed, but leave them for now.

        double s_xy = fitParameters.getEntry(1) * fitParameters.getEntry(1)
                * Math.pow(p.getDoubleValueForKey("pixelsize_nm"), 2);
        double s_z = fitParameters.getEntry(3) * fitParameters.getEntry(3)
                * Math.pow(p.getDoubleValueForKey("z_sectionsize_nm"), 2);

        //s_z = 0; //remove!!

        double error = (2 * s_xy + s_z) / (n_photons - 1);// + 4*Math.sqrt(Math.PI) * Math.pow(2*s_xy,1.5)*Math.pow(fitParameters.getEntry(6),2)/(p.getDoubleValueForKey("pixelsize_nm")*n_photons*n_photons);

        double b = fitParameters.getEntry(10);
        double a = p.getDoubleValueForKey("pixelsize_nm");
        double alpha = p.getDoubleValueForKey("z_sectionsize_nm");
        double sa_x = s_xy + Math.pow(a, 2) / 12;
        double sa_z = s_z + Math.pow(alpha, 2) / 12;
        double error_x = sa_x / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_x * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey("pixelsize_nm"), 2)));
        double error_z = sa_z / n_photons * (16.0 / 9.0 + 8 * Math.PI * sa_z * b * b
                / (n_photons * Math.pow(p.getDoubleValueForKey("z_sectionsize_nm"), 2)));

        double A = 1.0 / (2 * Math.sqrt(2) * Math.pow(Math.PI, 1.5) * Math.sqrt(sa_z) * sa_x);

        ErrIntFunc eif = new ErrIntFunc();

        eif.setParams(b, n_photons, A, sa_z, sa_x, a, alpha);

        //LegendreGaussIntegrator lgi = new LegendreGaussIntegrator(5, 10, 1000);

        //integrate over 10*width of PSF in z 

        //double size = 10*Math.sqrt(sa_z);

        //double intpart = 0;
        //            try {
        //               
        //               if (b < 0) throw new ConvergenceException(new DummyLocalizable("negative background!")); // a negative value for b seems to cause the integration to hang, preventing the program from progressing
        //               
        //               intpart = lgi.integrate(10000, eif, -size, size);
        //               
        //               double fullIntPart = intpart + Math.pow(2*Math.PI, 1.5)*sa_x*A/Math.sqrt(sa_z);
        //               
        //               error_x = Math.sqrt(2/(n_photons*sa_z/(2*sa_z + sa_x)*fullIntPart));
        //               error_z = Math.sqrt(2/(n_photons*sa_x/(2*sa_z + sa_x)*fullIntPart));
        //               
        //            } catch (ConvergenceException e) {
        //               LoggingUtilities.getLogger().severe("Integration error: " + e.getMessage());
        //               error_x = -1;
        //               error_z = -1;
        //            }
        //            

        if (error_x > 0 && error_z > 0) {

            error = Math.sqrt(2 * error_x * error_x + error_z * error_z);

        } else {
            error = Double.NaN;
        }

        error = 0; // until a better error calculation

        this.fitErrorByChannel.add(error);

        this.positionsByChannel.add(fitParameters.getSubVector(7, 3));

        this.nPhotonsByChannel.add(n_photons);

    }

    this.hadFittingError = false;
    this.nullifyImages();
}

From source file:org.apache.predictionio.examples.java.recommendations.tutorial1.Algorithm.java

@Override
public Model train(TrainingData data) {
    // pre-process
    Map<Integer, Map<Integer, Float>> itemMap = new HashMap<Integer, Map<Integer, Float>>();
    Map<Integer, Integer> userIndexMap = new HashMap<Integer, Integer>();
    Map<Integer, Integer> itemIndexMap = new HashMap<Integer, Integer>();

    int itemIndex = 0;
    int userIndex = 0;
    for (TrainingData.Rating r : data.ratings) {
        Map<Integer, Float> userRating = itemMap.get(r.iid);
        if (userRating == null) {
            // new item
            userRating = new HashMap<Integer, Float>();
            itemMap.put(r.iid, userRating);
            itemIndexMap.put(r.iid, itemIndex);
            itemIndex += 1; // increment item index for next item
        }//from w  w w  .  ja  v a2 s.c  om
        userRating.put(r.uid, r.rating);

        // update user index
        Integer u = userIndexMap.get(r.uid);
        if (u == null) {
            // new user
            userIndexMap.put(r.uid, userIndex);
            userIndex += 1;
        }
    }

    int numOfItems = itemIndexMap.size();
    int numOfUsers = userIndexMap.size();

    Map<Integer, RealVector> itemVectors = new HashMap<Integer, RealVector>();
    Map<Integer, RealVector> userHistory = new HashMap<Integer, RealVector>();

    for (Map.Entry<Integer, Map<Integer, Float>> entry : itemMap.entrySet()) {
        Integer itemID = entry.getKey();
        Integer iindex = itemIndexMap.get(itemID);
        Map<Integer, Float> userRatingMap = entry.getValue();
        RealVector item = new ArrayRealVector(numOfUsers); // dimension is numOfUsers
        for (Map.Entry<Integer, Float> r : userRatingMap.entrySet()) {
            Integer userID = r.getKey();
            Float rating = r.getValue();
            Integer uindex = userIndexMap.get(userID);
            item.setEntry(uindex, rating);
            // update user History
            RealVector user = userHistory.get(userID);
            if (user == null) {
                user = new OpenMapRealVector(numOfItems);
                userHistory.put(userID, user);
            }
            user.setEntry(iindex, rating);
        }
        itemVectors.put(itemID, item);
    }

    // calculate sim

    Map<Integer, RealVector> itemSimilarity = new HashMap<Integer, RealVector>();
    List<Integer> item1List = new ArrayList<Integer>(itemIndexMap.keySet());
    List<Integer> item2List = new ArrayList<Integer>(item1List);

    int numSimilarItems = 100;
    Comparator<IndexAndScore> comparator = new IndexAndScoreComparator();
    Map<Integer, Queue<IndexAndScore>> topItemSimilarity = new HashMap<Integer, Queue<IndexAndScore>>();

    for (Integer itemID1 : item1List) {
        item2List.remove(0);
        Integer index1 = itemIndexMap.get(itemID1);
        for (Integer itemID2 : item2List) {
            RealVector vector1 = itemVectors.get(itemID1);
            RealVector vector2 = itemVectors.get(itemID2);
            double score = vector1.cosine(vector2);
            if (score > params.threshold) {
                Integer index2 = itemIndexMap.get(itemID2);
                setTopItemSimilarity(topItemSimilarity, itemID1, index2, score, numSimilarItems, comparator);
                setTopItemSimilarity(topItemSimilarity, itemID2, index1, score, numSimilarItems, comparator);
            }
        }
    }

    for (Map.Entry<Integer, Queue<IndexAndScore>> entry : topItemSimilarity.entrySet()) {
        Iterator<IndexAndScore> it = entry.getValue().iterator();
        RealVector vector = new OpenMapRealVector(numOfItems);
        while (it.hasNext()) {
            IndexAndScore d = it.next();
            vector.setEntry(d.index, d.score);
        }
        itemSimilarity.put(entry.getKey(), vector);
    }

    return new Model(itemSimilarity, userHistory);
}

From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.CollaborativeFilteringAlgorithm.java

@Override
public CollaborativeFilteringModel train(PreparedData data) {
    // pre-process
    Map<Integer, Map<Integer, Float>> itemMap = new HashMap<Integer, Map<Integer, Float>>();
    Map<Integer, Integer> userIndexMap = new HashMap<Integer, Integer>();
    Map<Integer, Integer> itemIndexMap = new HashMap<Integer, Integer>();

    int itemIndex = 0;
    int userIndex = 0;
    for (TrainingData.Rating r : data.ratings) {
        Map<Integer, Float> userRating = itemMap.get(r.iid);
        if (userRating == null) {
            // new item
            userRating = new HashMap<Integer, Float>();
            itemMap.put(r.iid, userRating);
            itemIndexMap.put(r.iid, itemIndex);
            itemIndex += 1; // increment item index for next item
        }// w ww .  j ava2  s . com
        userRating.put(r.uid, r.rating);

        // update user index
        Integer u = userIndexMap.get(r.uid);
        if (u == null) {
            // new user
            userIndexMap.put(r.uid, userIndex);
            userIndex += 1;
        }
    }

    int numOfItems = itemIndexMap.size();
    int numOfUsers = userIndexMap.size();

    Map<Integer, RealVector> itemVectors = new HashMap<Integer, RealVector>();
    Map<Integer, RealVector> userHistory = new HashMap<Integer, RealVector>();

    for (Map.Entry<Integer, Map<Integer, Float>> entry : itemMap.entrySet()) {
        Integer itemID = entry.getKey();
        Integer iindex = itemIndexMap.get(itemID);
        Map<Integer, Float> userRatingMap = entry.getValue();
        RealVector item = new ArrayRealVector(numOfUsers); // dimension is numOfUsers
        for (Map.Entry<Integer, Float> r : userRatingMap.entrySet()) {
            Integer userID = r.getKey();
            Float rating = r.getValue();
            Integer uindex = userIndexMap.get(userID);
            item.setEntry(uindex, rating);
            // update user History
            RealVector user = userHistory.get(userID);
            if (user == null) {
                user = new OpenMapRealVector(numOfItems);
                userHistory.put(userID, user);
            }
            user.setEntry(iindex, rating);
        }
        itemVectors.put(itemID, item);
    }

    // calculate sim

    Map<Integer, RealVector> itemSimilarity = new HashMap<Integer, RealVector>();
    List<Integer> item1List = new ArrayList<Integer>(itemIndexMap.keySet());
    List<Integer> item2List = new ArrayList<Integer>(item1List);

    int numSimilarItems = 100;
    Comparator<IndexAndScore> comparator = new IndexAndScoreComparator();
    Map<Integer, Queue<IndexAndScore>> topItemSimilarity = new HashMap<Integer, Queue<IndexAndScore>>();

    for (Integer itemID1 : item1List) {
        item2List.remove(0);
        Integer index1 = itemIndexMap.get(itemID1);
        for (Integer itemID2 : item2List) {
            RealVector vector1 = itemVectors.get(itemID1);
            RealVector vector2 = itemVectors.get(itemID2);
            double score = vector1.cosine(vector2);
            if (score > params.threshold) {
                Integer index2 = itemIndexMap.get(itemID2);
                setTopItemSimilarity(topItemSimilarity, itemID1, index2, score, numSimilarItems, comparator);
                setTopItemSimilarity(topItemSimilarity, itemID2, index1, score, numSimilarItems, comparator);
            }
        }
    }

    for (Map.Entry<Integer, Queue<IndexAndScore>> entry : topItemSimilarity.entrySet()) {
        Iterator<IndexAndScore> it = entry.getValue().iterator();
        RealVector vector = new OpenMapRealVector(numOfItems);
        while (it.hasNext()) {
            IndexAndScore d = it.next();
            vector.setEntry(d.index, d.score);
        }
        itemSimilarity.put(entry.getKey(), vector);
    }

    return new CollaborativeFilteringModel(itemSimilarity, userHistory);
}

From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.Preparator.java

public PreparedData prepare(TrainingData trainingData) {
    Map<Integer, RealVector> itemFeatures = new HashMap<Integer, RealVector>();

    int featureSize = trainingData.genres.size();

    for (Integer iid : trainingData.itemInfo.keySet()) {
        String[] info = trainingData.itemInfo.get(iid);

        RealVector features = new ArrayRealVector(featureSize);
        for (int i = 0; i < featureSize; i++) {
            features.setEntry(i, Double.parseDouble(info[i + indexOffset]));
        }/*  ww w  . j av  a  2 s .  c om*/
        itemFeatures.put(iid, features);
    }

    return new PreparedData(trainingData, itemFeatures, featureSize);
}

From source file:org.briljantframework.array.Matrices.java

/**
 * View the double array as a {@link RealVector}.
 *
 * @param array the array/*  w  w  w .jav  a 2  s .c o  m*/
 * @return a real vector view
 */
public static RealVector asRealVector(DoubleArray array) {
    Check.argument(array.isVector(), CAN_ONLY_VIEW_1D_ARRAYS);
    return new RealVector() {
        @Override
        public int getDimension() {
            return array.size();
        }

        @Override
        public double getEntry(int index) throws OutOfRangeException {
            return array.get(index);
        }

        @Override
        public void setEntry(int index, double value) throws OutOfRangeException {
            array.set(index, value);
        }

        @Override
        public RealVector append(RealVector v) {
            ArrayRealVector vector = new ArrayRealVector(v.getDimension() + array.size());
            copyFromArray(vector);
            for (int i = 0; i < v.getDimension(); i++) {
                vector.setEntry(i, v.getEntry(i));
            }
            return vector;
        }

        private void copyFromArray(ArrayRealVector vector) {
            for (int i = 0; i < array.size(); i++) {
                vector.setEntry(i, array.get(i));
            }
        }

        @Override
        public RealVector append(double d) {
            ArrayRealVector vector = new ArrayRealVector(array.size() + 1);
            copyFromArray(vector);
            vector.setEntry(array.size(), d);
            return vector;
        }

        @Override
        public RealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException {
            return asRealVector(array.get(Range.of(index, index + n)));
        }

        @Override
        public void setSubVector(int index, RealVector v) throws OutOfRangeException {
            for (int i = 0; i < array.size(); i++) {
                array.set(i + index, v.getEntry(i));
            }
        }

        @Override
        public boolean isNaN() {
            return Arrays.any(array, Double::isNaN);
        }

        @Override
        public boolean isInfinite() {
            return Arrays.any(array, Double::isInfinite);
        }

        @Override
        public RealVector copy() {
            return asRealVector(array.copy());
        }

        @Override
        @Deprecated
        public RealVector ebeDivide(RealVector v) throws DimensionMismatchException {
            if (v.getDimension() != array.size()) {
                throw new DimensionMismatchException(v.getDimension(), array.size());
            }
            RealVector a = new ArrayRealVector(array.size());
            for (int i = 0; i < array.size(); i++) {
                a.setEntry(i, array.get(i) / v.getEntry(i));
            }
            return a;
        }

        @Override
        @Deprecated
        public RealVector ebeMultiply(RealVector v) throws DimensionMismatchException {
            if (v.getDimension() != array.size()) {
                throw new DimensionMismatchException(v.getDimension(), array.size());
            }
            RealVector a = new ArrayRealVector(array.size());
            for (int i = 0; i < array.size(); i++) {
                a.setEntry(i, array.get(i) * v.getEntry(i));
            }
            return a;
        }
    };
}

From source file:org.eclipse.dataset.LinearAlgebra.java

private static RealVector createRealVector(Dataset a) {
    if (a.getRank() != 1) {
        throw new IllegalArgumentException("Dataset must be rank 1");
    }/*w  w w. j a va 2s . c  om*/
    int size = a.getSize();
    IndexIterator it = a.getIterator(true);
    int[] pos = it.getPos();
    RealVector m = new ArrayRealVector(size);
    while (it.hasNext()) {
        m.setEntry(pos[0], a.getElementDoubleAbs(it.index));
    }
    return m;
}

From source file:org.grouplens.lenskit.diffusion.Iterative.IterativeDiffusionItemScorer.java

/**
 * Score items by computing predicted ratings.
 *
 * @see ItemScoreAlgorithm#scoreItems(ItemItemModel, org.grouplens.lenskit.vectors.SparseVector, org.grouplens.lenskit.vectors.MutableSparseVector, NeighborhoodScorer)
 *//*  w ww  .  jav  a 2  s  .co m*/
@Override
public void score(long user, @Nonnull MutableSparseVector scores) {
    UserHistory<? extends Event> history = dao.getEventsForUser(user, summarizer.eventTypeWanted());
    if (history == null) {
        history = History.forUser(user);
    }
    SparseVector summary = summarizer.summarize(history);
    VectorTransformation transform = normalizer.makeTransformation(user, summary);
    MutableSparseVector normed = summary.mutableCopy();
    transform.apply(normed);
    scores.clear();
    int numItems = 1682;
    //algorithm.scoreItems(model, normed, scores, scorer);
    int num_updates = 300;
    double update_rate = 1;
    double threshold = 0.01;
    RealVector z_out = diffusionMatrix.preMultiply(VectorUtils.toRealVector(numItems, normed));
    boolean updated = true;
    LongSortedSet known = normed.keySet();
    int count_iter = 0;
    for (int i = 0; i < num_updates && updated; i++) {
        updated = false;
        RealVector temp = diffusionMatrix.preMultiply(z_out);
        temp.mapMultiplyToSelf(z_out.getNorm() / temp.getNorm());
        RealVector temp_diff = z_out.add(temp.mapMultiplyToSelf(-1.0));
        for (int j = 0; j < numItems; j++) {
            if (!known.contains((long) (j + 1))) {
                //if the rating is not one of the known ones
                if (Math.abs(temp_diff.getEntry(j)) > threshold) {
                    // if difference is large enough, update
                    updated = true;
                    z_out.setEntry(j, (1.0 - update_rate) * z_out.getEntry(j) + update_rate * temp.getEntry(j));
                }
            }
        }
        count_iter++;
    }
    System.out.println(count_iter);
    LongSortedSet testDomain = scores.keyDomain();
    //fill up the score vector
    for (int i = 0; i < numItems; i++) {
        if (testDomain.contains((long) (i + 1))) {
            scores.set((long) (i + 1), z_out.getEntry(i));
        }
    }

    // untransform the scores
    transform.unapply(scores);
    System.out.println(scores);
}

From source file:org.grouplens.samantha.modeler.featurizer.PercentileModel.java

public PercentileModel buildModel(String attrName, int numValues, EntityDAO entityDAO) {
    RealVector values = MatrixUtils.createRealVector(new double[dim]);
    Double2IntSortedMap sortedMap = new Double2IntAVLTreeMap();
    int total = 0;
    Random random = new Random();
    while (entityDAO.hasNextEntity()) {
        if (random.nextDouble() > sampleRate) {
            entityDAO.getNextEntity();//  w  w w.  j a  v a 2s. c  om
            continue;
        }
        double value = entityDAO.getNextEntity().get(attrName).asDouble();
        if (sortedMap.containsKey(value)) {
            sortedMap.put(value, sortedMap.get(value) + 1);
        } else {
            sortedMap.put(value, 1);
        }
        total++;
    }
    values.setEntry(0, numValues);
    int idx = 0;
    int cnt = 0;
    for (Double2IntMap.Entry entry : sortedMap.double2IntEntrySet()) {
        double key = entry.getDoubleKey();
        int num = entry.getIntValue();
        cnt += num;
        double value = cnt * 1.0 / total;
        double point = idx * 1.0 / numValues;
        while (point <= value) {
            values.setEntry(idx * 2 + 1, key);
            values.setEntry(idx * 2 + 2, point);
            idx++;
            point = idx * 1.0 / numValues;
        }
    }
    ensureKey(attrName);
    setKeyVector(attrName, values);
    return this;
}

From source file:org.grouplens.samantha.modeler.knn.FeatureKnnModel.java

public FeatureKnnModel buildModel() {
    List<String> features = Lists.newArrayList(svdFeature.getFactorFeatures(minSupport).keySet());
    IntList svdIndices = new IntArrayList();
    IntList simIndices = new IntArrayList(features.size());
    for (int i = 0; i < features.size(); i++) {
        String feature = features.get(i);
        Map<String, String> attrVals = FeatureExtractorUtilities.decomposeKey(feature);
        boolean ifModel = true;
        if (attrVals.size() == feaAttrs.size()) {
            for (String attr : feaAttrs) {
                if (!attrVals.containsKey(attr)) {
                    ifModel = false;/*from  w  w  w . j  a  v  a  2s.  com*/
                }
            }
        } else {
            ifModel = false;
        }
        if (ifModel) {
            svdIndices.add(i);
            ensureKey(feature);
            simIndices.add(getIndexByKey(feature));
        } else {
            simIndices.add(-1);
        }
    }
    Logger.info("Total number of items to compute similarity model {}: {}", modelName, svdIndices.size());
    svdIndices.parallelStream().forEach(curIdx -> {
        int simIdx = simIndices.getInt(curIdx);
        List<double[]> neighbors = getNeighbors(curIdx, svdIndices, svdFeature);
        RealVector sims = getIndexVector(simIdx);
        for (int j = 0; j < neighbors.size(); j++) {
            double[] neighbor = neighbors.get(j);
            sims.setEntry(j * 2, simIndices.getInt((int) neighbor[0]));
            sims.setEntry(j * 2 + 1, neighbor[1]);
        }
        setIndexVector(simIdx, sims);
    });
    return this;
}