Example usage for com.google.common.collect HashBasedTable create

List of usage examples for com.google.common.collect HashBasedTable create

Introduction

In this page you can find the example usage for com.google.common.collect HashBasedTable create.

Prototype

public static <R, C, V> HashBasedTable<R, C, V> create() 

Source Link

Document

Creates an empty HashBasedTable .

Usage

From source file:librec.rating.GPLSA.java

@Override
protected void initModel() throws Exception {

    // Pz_u//from   w  ww.j a v  a2 s. c o m
    Puk = new DenseMatrix(numUsers, numFactors);
    for (int u = 0; u < numUsers; u++) {
        double[] probs = Randoms.randProbs(numFactors);
        for (int k = 0; k < numFactors; k++) {
            Puk.set(u, k, probs[k]);
        }
    }

    // normalize ratings
    double mean = globalMean;
    double sd = Stats.sd(trainMatrix.getData(), mean);

    q = algoOptions.getFloat("-q");
    b = algoOptions.getFloat("-b", 1.0f);

    mu = new DenseVector(numUsers);
    sigma = new DenseVector(numUsers);
    for (int u = 0; u < numUsers; u++) {
        SparseVector ru = trainMatrix.row(u);
        int Nu = ru.size();
        if (Nu < 1)
            continue;

        // compute mu_u
        double mu_u = (ru.sum() + q * mean) / (Nu + q);
        mu.set(u, mu_u);

        // compute sigma_u
        double sum = 0;
        for (VectorEntry ve : ru) {
            sum += Math.pow(ve.get() - mu_u, 2);
        }
        sum += q * Math.pow(sd, 2);
        double sigma_u = Math.sqrt(sum / (Nu + q));
        sigma.set(u, sigma_u);
    }

    // initialize Q
    Q = HashBasedTable.create();

    for (MatrixEntry me : trainMatrix) {
        int u = me.row();
        int i = me.column();
        double rate = me.get();

        double r = (rate - mu.get(u)) / sigma.get(u); // continuous ratings
        me.set(r);

        Q.put(u, i, new HashMap<Integer, Double>());
    }

    // initialize Mu, Sigma
    Mu = new DenseMatrix(numItems, numFactors);
    Sigma = new DenseMatrix(numItems, numFactors);
    for (int i = 0; i < numItems; i++) {
        SparseVector ci = trainMatrix.column(i);
        int Ni = ci.size();

        if (Ni < 1)
            continue;

        double mu_i = ci.mean();

        double sum = 0;
        for (VectorEntry ve : ci) {
            sum += Math.pow(ve.get() - mu_i, 2);
        }
        double sd_i = Math.sqrt(sum / Ni);

        for (int z = 0; z < numFactors; z++) {
            Mu.set(i, z, mu_i + smallValue * Math.random());
            Sigma.set(i, z, sd_i + smallValue * Math.random());
        }
    }
}

From source file:de.uni_potsdam.hpi.asg.delaymatch.setup.MeasureRecordGenerator.java

public MeasureRecordGenerator(Map<String, DelayMatchModule> modules, File file, VerilogModule rootModule) {
    this.modules = modules;
    this.file = file;
    this.rootModule = rootModule;
    this.transtable = HashBasedTable.create();
}

From source file:com.continuuity.weave.internal.appmaster.RunningContainers.java

RunningContainers(String appId, WeaveRunResources appMasterResources) {
    containers = HashBasedTable.create();
    runnableInstances = Maps.newHashMap();
    startSequence = Lists.newLinkedList();
    containerLock = new ReentrantLock();
    containerChange = containerLock.newCondition();
    resourceReport = new DefaultResourceReport(appId, appMasterResources);
}

From source file:com.przemo.etl.dataproviders.CSVDataProvider.java

/**
 * Reads data from a given file. Headers array gets populated either with default ones or with read in from the file, if so requested.
 * @return//from  ww  w.  j a va 2 s.  c  o m
 * @throws Exception
 */
@Override
public Table readData() {
    Table data = HashBasedTable.create();
    if (file.exists() && file.canRead()) {
        try {
            BufferedReader r = new BufferedReader(new FileReader(file));
            String line;
            boolean dhReadIn = false;
            int rowNumber = 0;
            while ((line = r.readLine()) != null) {
                String[] lineArray = line.split(sep);
                if (!dhReadIn) {
                    if (dh) {
                        headers = lineArray;
                    } else {
                        headers = defaultHeaders(lineArray.length);
                    }
                    dhReadIn = true;
                } else {
                    for (int c = 0; c < lineArray.length; c++) {
                        data.put(rowNumber, headers[c], lineArray[c]);
                    }
                }
                rowNumber++;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CSVDataProvider.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CSVDataProvider.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(CSVDataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    return data;
}

From source file:i5.las2peer.services.recommender.librec.data.CSVDataDAO.java

/**
 * Read data from the data file. Note that we didn't take care of the duplicated lines.
 * /* w  ww  .  j  a  va2s.  co  m*/
 * @param cols
 *            the indexes of the relevant columns in the data file: {user, item, [rating, timestamp] (optional)}
 * @param binThold
 *            the threshold to binarize a rating. If a rating is greater than the threshold, the value will be 1;
 *            otherwise 0. To disable this feature, i.e., keep the original rating value, set the threshold a
 *            negative value
 * @return a sparse matrix storing all the relevant data
 */
public SparseMatrix[] readData(int[] cols, double binThold) throws Exception {

    Logs.info(String.format("Dataset: %s", Strings.last(dataPath, 38)));

    // Table {row-id, col-id, rate}
    Table<Integer, Integer, Double> dataTable = HashBasedTable.create();
    // Table {row-id, col-id, timestamp}
    Table<Integer, Integer, Long> timeTable = null;
    // Map {col-id, multiple row-id}: used to fast build a rating matrix
    Multimap<Integer, Integer> colMap = HashMultimap.create();

    BufferedReader br = FileIO.getReader(dataPath);
    String line = null;
    minTimestamp = Long.MAX_VALUE;
    maxTimestamp = Long.MIN_VALUE;
    while ((line = br.readLine()) != null) {
        if (isHeadline()) {
            setHeadline(false);
            continue;
        }

        String[] data = line.trim().split("[ \t,]+");

        if (data.length < 2) {
            Logs.error(String.format("Dataset: Cannot read line \"%s\"", line));
            continue;
        }

        String user = data[cols[0]];
        String item = data[cols[1]];
        Double rate = (cols.length >= 3 && data.length >= 3) ? Double.valueOf(data[cols[2]]) : 1.0;

        // binarize the rating for item recommendation task
        if (binThold >= 0)
            rate = rate > binThold ? 1.0 : 0.0;

        scaleDist.add(rate);

        // inner id starting from 0
        int row = userIds.containsKey(user) ? userIds.get(user) : userIds.size();
        userIds.put(user, row);

        int col = itemIds.containsKey(item) ? itemIds.get(item) : itemIds.size();
        itemIds.put(item, col);

        dataTable.put(row, col, rate);
        colMap.put(col, row);

        // record rating's issuing time
        if (cols.length >= 4 && data.length >= 4) {
            if (timeTable == null)
                timeTable = HashBasedTable.create();

            // convert to million-seconds
            long mms = 0L;
            try {
                mms = Long.parseLong(data[cols[3]]); // cannot format "9.7323480e+008"
            } catch (NumberFormatException e) {
                mms = (long) Double.parseDouble(data[cols[3]]);
            }
            long timestamp = timeUnit.toMillis(mms);

            if (minTimestamp > timestamp)
                minTimestamp = timestamp;

            if (maxTimestamp < timestamp)
                maxTimestamp = timestamp;

            timeTable.put(row, col, timestamp);
        }

    }
    br.close();

    numRatings = scaleDist.size();
    ratingScale = new ArrayList<>(scaleDist.elementSet());
    Collections.sort(ratingScale);

    int numRows = numUsers(), numCols = numItems();

    // if min-rate = 0.0, shift upper a scale
    double minRate = ratingScale.get(0).doubleValue();
    double epsilon = minRate == 0.0 ? ratingScale.get(1).doubleValue() - minRate : 0;
    if (epsilon > 0) {
        // shift upper a scale
        for (int i = 0, im = ratingScale.size(); i < im; i++) {
            double val = ratingScale.get(i);
            ratingScale.set(i, val + epsilon);
        }
        // update data table
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                if (dataTable.contains(row, col))
                    dataTable.put(row, col, dataTable.get(row, col) + epsilon);
            }
        }
    }

    String dateRange = "";
    if (cols.length >= 4)
        dateRange = String.format(", Timestamps = {%s, %s}", Dates.toString(minTimestamp),
                Dates.toString(maxTimestamp));

    Logs.debug("With Specs: {Users, {}} = {{}, {}, {}}, Scale = {{}}{}",
            (isItemAsUser ? "Users, Links" : "Items, Ratings"), numRows, numCols, numRatings,
            Strings.toString(ratingScale), dateRange);

    // build rating matrix
    rateMatrix = new SparseMatrix(numRows, numCols, dataTable, colMap);

    if (timeTable != null)
        timeMatrix = new SparseMatrix(numRows, numCols, timeTable, colMap);

    // release memory of data table
    dataTable = null;
    timeTable = null;

    return new SparseMatrix[] { rateMatrix, timeMatrix };
}

From source file:librec.undefined.TimeSVDPlusPlus.java

@Override
protected void initModel() throws Exception {
    super.initModel();

    userBias = new DenseVector(numUsers);
    userBias.init();//from ww  w  .  j  a  v  a2  s  . co  m

    itemBias = new DenseVector(numItems);
    itemBias.init();

    userAlpha = new DenseVector(numUsers);
    userAlpha.init();

    Bit = new DenseMatrix(numItems, numBins);
    Bit.init();

    userScaling = new DenseVector(numUsers); // cu
    userScaling.init();

    Y = new DenseMatrix(numItems, numFactors);
    Y.init();

    Auf = new DenseMatrix(numUsers, numFactors);
    Auf.init();

    But = HashBasedTable.create();
    Cut = HashBasedTable.create();
    Puft = new HashMap<>();
}

From source file:hu.ppke.itk.nlpg.purepos.decoder.BeamedViterbi.java

public List<Pair<List<Integer>, Double>> beamedSearch(final NGram<Integer> start,
        final List<String> observations, int resultsNumber) {
    HashMap<NGram<Integer>, Node> beam = new HashMap<NGram<Integer>, Node>();

    beam.put(start, startNode(start));//from   ww  w  . j  a v  a 2s . c o m
    boolean isFirst = true;
    int pos = 0;
    for (String obs : observations) {
        // System.err.println(obs);

        // logger.trace("Current observation " + obs);
        // logger.trace("\tCurrent states:");
        // for (Entry<NGram<Integer>, Node> entry : beam.entrySet()) {
        // logger.trace("\t\t" + entry.getKey() + " - " + entry.getValue());
        // }

        HashMap<NGram<Integer>, Node> newBeam = new HashMap<NGram<Integer>, Node>();

        Table<NGram<Integer>, Integer, Double> nextProbs = HashBasedTable.create();
        Map<NGram<Integer>, Double> obsProbs = new HashMap<NGram<Integer>, Double>();
        Set<NGram<Integer>> contexts = beam.keySet();

        Map<NGram<Integer>, Map<Integer, Pair<Double, Double>>> nexts = getNextProbs(contexts, obs, pos,
                isFirst);

        for (Map.Entry<NGram<Integer>, Map<Integer, Pair<Double, Double>>> nextsEntry : nexts.entrySet()) {
            NGram<Integer> context = nextsEntry.getKey();
            Map<Integer, Pair<Double, Double>> nextContextProbs = nextsEntry.getValue();
            for (Map.Entry<Integer, Pair<Double, Double>> entry : nextContextProbs.entrySet()) {
                Integer tag = entry.getKey();
                nextProbs.put(context, tag, entry.getValue().getLeft());
                obsProbs.put(context.add(tag), entry.getValue().getRight());
            }
        }
        // for (Integer t : nextProbs.keySet()) {
        // logger.trace("\t\tNext node:" + context + t);
        // logger.trace("\t\tnode currentprob:"
        // + (beam.get(context) + nextProbs.get(t).getLeft()));
        // logger.trace("\t\tnode emissionprob:"
        // + nextProbs.get(t).getRight());
        // logger.trace("\n");
        // // logger.trace("\t\tNext node:" + context + t);
        // }
        for (Cell<NGram<Integer>, Integer, Double> cell : nextProbs.cellSet()) {
            Integer nextTag = cell.getColumnKey();
            NGram<Integer> context = cell.getRowKey();
            Double transVal = cell.getValue();
            NGram<Integer> newState = context.add(nextTag);
            Node from = beam.get(context);
            double newVal = transVal + beam.get(context).getWeight();
            update(newBeam, newState, newVal, from);
        }
        // adding observation probabilities
        // logger.trace("beam" + newBeam);
        if (nextProbs.size() > 1)
            for (NGram<Integer> tagSeq : newBeam.keySet()) {
                // Integer tag = tagSeq.getLast();
                Node node = newBeam.get(tagSeq);
                // Double prevVal = node.getWeight();

                Double obsProb = obsProbs.get(tagSeq);
                // logger.trace("put to beam: " + context + "(from) "
                // + tagSeq + " " + prevVal + "+" + obsProb);
                node.setWeight(obsProb + node.getWeight());
            }

        beam = prune(newBeam);
        isFirst = false;
        // for (Entry<NGram<Integer>, Node> e : beam.entrySet()) {
        // logger.trace("\t\tNode state: " + e.getKey() + " "
        // + e.getValue());
        // }
        ++pos;
    }
    return findMax(beam, resultsNumber);
}

From source file:com.sk89q.worldedit.world.block.BlockState.java

private void populate(Map<Map<Property<?>, Object>, BlockState> stateMap) {
    final Table<Property<?>, Object, BlockState> states = HashBasedTable.create();

    for (final Map.Entry<Property<?>, Object> entry : this.values.entrySet()) {
        final Property<Object> property = (Property<Object>) entry.getKey();

        property.getValues().forEach(value -> {
            if (value != entry.getValue()) {
                BlockState modifiedState = stateMap.get(this.withValue(property, value));
                if (modifiedState != null) {
                    states.put(property, value, modifiedState);
                } else {
                    System.out.println(stateMap);
                    WorldEdit.logger.warn("Found a null state at " + this.withValue(property, value));
                }//from w w  w .  ja v a2 s  .  com
            }
        });
    }

    this.states = states.isEmpty() ? states : ArrayTable.create(states);
}

From source file:es.usc.citius.composit.core.composition.network.AbstractServiceMatchNetwork.java

public Map<Operation<E>, Table<E, E, T>> getSourceOperationsThatMatch(Operation<E> target) {
    // First, compute the source elements that match the op.inputs
    Map<Operation<E>, Table<E, E, T>> matchMap = new HashMap<Operation<E>, Table<E, E, T>>();
    for (E targetInput : target.getSignature().getInputs()) {
        Map<E, T> sourceMatch = getSourceElementsThatMatch(targetInput);
        // Find the providers
        for (Map.Entry<E, T> sourceMatchEntry : sourceMatch.entrySet()) {
            E sourceOutput = sourceMatchEntry.getKey();
            Set<Operation<E>> sourceOps = Sets.newHashSet(getOperationsWithOutput(sourceOutput));
            // Annotate source output->target input match
            for (Operation<E> op : sourceOps) {
                Table<E, E, T> matchTable = matchMap.get(op);
                if (matchTable == null) {
                    matchTable = HashBasedTable.create();
                    matchMap.put(op, matchTable);
                }//www  .  j av  a2s  .c  o  m
                // Add match entry
                matchTable.put(sourceOutput, targetInput, sourceMatchEntry.getValue());
            }
        }
    }
    return matchMap;
}

From source file:org.mousephenotype.dcc.exportlibrary.traverser.Reporter.java

public Reporter(CommandImpl validationResultsExtractor, HibernateManager hibernateManager) {
    this.validationResultsExtractor = validationResultsExtractor;
    this.hibernateManager = hibernateManager;
    this.submissions = HashBasedTable.create();
}