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

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

Introduction

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

Prototype

@Override
    public V get(@Nullable Object rowKey, @Nullable Object columnKey) 

Source Link

Usage

From source file:carskit.alg.cars.adaptation.dependent.dev.GCSLIM_CC.java

@Override
protected void buildModel() throws Exception {

    // number of iteration cycles
    for (int iter = 1; iter <= numIters; iter++) {

        loss = 0;/*from  w  w  w  .j a va 2s . co m*/

        for (MatrixEntry me : trainMatrix) {

            int ui = me.row(); // user-item
            int u = rateDao.getUserIdFromUI(ui);
            int j = rateDao.getItemIdFromUI(ui);
            int c = me.column(); // context
            double rujc = me.get();

            Collection<Integer> nns = knn > 0 ? itemNNs.get(j) : allItems;
            SparseVector Ru = userCache.get(u);

            HashBasedTable<Integer, Integer, Double> Dev_weights = HashBasedTable.create();
            HashBasedTable<Integer, Integer, Double> Weight_devs = HashBasedTable.create();

            double pred = 0;
            for (int k : nns) {
                if (Ru.contains(k)) {
                    if (k != j) {
                        // extract a random contextual rating by user u and item k
                        String key = u + "," + k;
                        int uiid = rateDao.getUserItemId(key);
                        List<Integer> ctxid = this.trainMatrix.getColumns(uiid);

                        Random r = new Random();
                        int index = r.nextInt(ctxid.size());
                        int ctx = ctxid.get(index);

                        // get rating for u, k, ctx
                        double ruk = this.trainMatrix.get(uiid, ctx);

                        String[] sfrom = rateDao.getContextId(ctx).split(",");
                        String[] sto = rateDao.getContextId(c).split(",");
                        double dev_c = 0;
                        double w = W.get(k, j);
                        for (int i = 0; i < sfrom.length; ++i) {
                            int cond1 = Integer.valueOf(sfrom[i]);
                            int cond2 = Integer.valueOf(sto[i]);
                            dev_c += ccDev.get(cond1, cond2);
                            if (cond1 != cond2) {
                                if (Dev_weights.contains(cond1, cond2))
                                    Dev_weights.put(cond1, cond2, w + Dev_weights.get(cond1, cond2));
                                else
                                    Dev_weights.put(cond1, cond2, w);
                            }
                        }
                        Weight_devs.put(k, j, dev_c + ruk);
                        pred += (ruk + dev_c) * w;

                    }

                }
            }

            double eujc = rujc - pred;
            loss += eujc * eujc;

            for (int idk : Weight_devs.rowKeySet())
                for (int idj : Weight_devs.row(idk).keySet()) {

                    double update = W.get(idk, idj);

                    loss += regLw2 * update * update + regLw1 * update;

                    double delta_w = eujc * Weight_devs.get(idk, idj) - regLw2 * update - regLw1;
                    update += lRate * delta_w;
                    W.set(idk, idj, update);

                }

            // start updating cDev
            for (int cond1 : Dev_weights.rowKeySet())
                for (int cond2 : Dev_weights.row(cond1).keySet()) {

                    double update = ccDev.get(cond1, cond2);
                    loss += regLc2 * update * update + regLc1 * update;

                    double delta_c = eujc * Dev_weights.get(cond1, cond2) - regLc2 * update - regLc1;
                    update += lRate * delta_c;
                    ccDev.set(cond1, cond2, update);

                }

        }

    }
}

From source file:com.splout.db.benchmark.TablespaceAnalyserCMD.java

public int start() throws Exception {
    JSONTablespaceDefinition def = loadTablespaceFile(tablespaceFile);
    String tsName = def.getName();

    SploutClient client = new SploutClient(1000 * 60 * 60 * 24, qNodes.split(","));
    QNodeStatus overview = client.overview();

    if (overview.getTablespaceMap().get(tsName) == null) {
        System.out.println("Tablespace " + tsName + " not found in QNodes " + qNodes + ".");
    }//  ww  w  .j  a  v a  2s . c  o m

    Tablespace tablespace = overview.getTablespaceMap().get(tsName);
    int nPartitions = tablespace.getPartitionMap().getPartitionEntries().size();

    System.out.println("TABLESPACE [" + tsName + "]");
    System.out.println("#Partitions: " + nPartitions);

    HashBasedTable<Integer, String, Long> counts = HashBasedTable.create();
    HashBasedTable<Integer, String, LinkedHashMap<String, Long>> tops = HashBasedTable.create();
    for (int part = 0; part < nPartitions; part++) {
        if (partitions.size() > 0 && !partitions.contains(part)) {
            continue;
        }

        for (int i = 0; i < def.getPartitionedTables().size(); i++) {
            JSONTablespaceDefinition.JSONTableDefinition table = def.getPartitionedTables().get(i);

            String tblName = table.getName();
            String query = "SELECT COUNT(*) FROM " + tblName;

            QueryStatus status = client.query(tsName, null, query, part + "");
            if (status.getError() != null) {
                throw new Exception("Query error: " + status.getError());
            }
            System.out.println(query + ": " + JSONSerDe.ser(status));
            long count = (Integer) ((Map) status.getResult().get(0)).values().iterator().next();
            counts.put(part, tblName, count);

            String partFields[] = table.getPartitionFields().split(",");
            String concatFields = Joiner.on("||").join(partFields);
            query = "SELECT " + concatFields + " key, COUNT(*) c FROM " + tblName
                    + " GROUP BY key ORDER by c DESC LIMIT " + topSize;

            status = client.query(tsName, null, query, part + "");
            if (status.getError() != null) {
                throw new Exception("Query error: " + status.getError());
            }
            System.out.println(query + ": " + JSONSerDe.ser(status));
            LinkedHashMap<String, Long> top = new LinkedHashMap<String, Long>();
            for (Map row : (ArrayList<Map<String, Long>>) status.getResult()) {
                top.put(row.get("key").toString(), new Long(row.get("c").toString()));
            }
            tops.put(part, tblName, top);
        }
    }

    Hashtable<String, Long> totalsPerTable = new Hashtable<String, Long>();
    for (String table : counts.columnKeySet()) {
        long count = 0;
        for (Map.Entry<Integer, Long> entry : counts.column(table).entrySet()) {
            count += entry.getValue();
        }
        totalsPerTable.put(table, count);
    }

    BufferedWriter countsFile = Files.newWriter(new File(tsName + "-counts.txt"), Charset.defaultCharset());
    countsFile.write("Table\tPartition\tRows\tPercent from total rows\n");
    for (String table : counts.columnKeySet()) {
        for (int partition : counts.column(table).keySet()) {
            long count = counts.get(partition, table);
            long total = totalsPerTable.get(table);
            double percent = count / (double) total;
            countsFile.write(table + "\t" + partition + "\t" + count + "\t" + percent + "\n");
        }
    }
    countsFile.close();

    BufferedWriter topsFile = Files.newWriter(new File(tsName + "-tops.txt"), Charset.defaultCharset());
    topsFile.write("Table\tPartition\tKey\tRows\tPercent from total rows\n");
    for (String table : tops.columnKeySet()) {
        for (int partition : tops.column(table).keySet()) {
            long total = totalsPerTable.get(table);
            LinkedHashMap<String, Long> top = tops.get(partition, table);

            for (Map.Entry<String, Long> entry : top.entrySet()) {
                double percent = entry.getValue() / (double) total;
                topsFile.write(table + "\t" + partition + "\t" + entry.getKey() + "\t" + entry.getValue() + "\t"
                        + percent + "\n");
            }
        }
    }
    topsFile.close();

    return 0;
}