@Override public Response serve() { Frame fr = DKV.get(data_key.value()).get(); if (fr == null) return RequestServer._http404.serve(); // Build a frame with the selected Vecs Frame fr2 = new Frame(new String[0], new Vec[0]); int[] idxs = vecs.value(); for (int idx : idxs) // The selected frame columns fr2.add(fr._names[idx], fr._vecs[idx]); // Add the class-vec last Vec cvec = class_vec.value(); fr2.add(fr._names[class_vec._colIdx.get()], cvec); domain = cvec.domain(); // Class/enum/factor names mtrys = features.value() == null ? (int) (Math.sqrt(idxs.length) + 0.5) : features.value(); DRF drf = DRF.start( DRF.makeKey(), fr2, depth.value(), ntrees.value(), mtrys, sample_rate.value(), seed.value()); drf.get(); // Block for result cm = drf.cm(); // Get CM result return new Response(Response.Status.done, this, -1, -1, null); }
/** * Adds terms and frequencies found in vector into the Map termFreqMap * * @param termFreqMap a Map of terms and their frequencies * @param vector List of terms and their frequencies for a doc/field * @param fieldName Optional field name of the terms for skip terms */ private void addTermFrequencies( Map<String, Int> termFreqMap, Terms vector, @Nullable String fieldName) throws IOException { final TermsEnum termsEnum = vector.iterator(); final CharsRefBuilder spare = new CharsRefBuilder(); BytesRef text; while ((text = termsEnum.next()) != null) { spare.copyUTF8Bytes(text); final String term = spare.toString(); if (isNoiseWord(term)) { continue; } if (isSkipTerm(fieldName, term)) { continue; } final PostingsEnum docs = termsEnum.postings(null, null); int freq = 0; while (docs != null && docs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { freq += docs.freq(); } // increment frequency Int cnt = termFreqMap.get(term); if (cnt == null) { cnt = new Int(); termFreqMap.put(term, cnt); cnt.x = freq; } else { cnt.x += freq; } } }
@Test public void testReplaceMin() { int capacity = 10; MinHeap minHeap = new MinHeap(new IntFactory(), capacity); for (int i = 0; i < capacity; i++) { minHeap.insert(new Int(i)); } assertEquals(capacity, minHeap.getNumEntries()); assertFalse(minHeap.isEmpty()); for (int i = capacity; i < capacity * 2; i++) { minHeap.replaceMin(new Int(i)); } assertEquals(capacity, minHeap.getNumEntries()); assertFalse(minHeap.isEmpty()); Int minI = new Int(); Int peekI = new Int(); int i = 0; while (!minHeap.isEmpty()) { minHeap.peekMin(peekI); minHeap.getMin(minI); assertTrue(peekI.compareTo(minI) == 0); assertEquals(i++ + capacity, minI.i); } }
/** * Adds terms and frequencies found in vector into the Map termFreqMap * * @param field2termFreqMap a Map of terms and their frequencies per field * @param vector List of terms and their frequencies for a doc/field */ private void addTermFrequencies( Map<String, Map<String, Int>> field2termFreqMap, Terms vector, String fieldName) throws IOException { Map<String, Int> termFreqMap = field2termFreqMap.get(fieldName); if (termFreqMap == null) { termFreqMap = new HashMap<>(); field2termFreqMap.put(fieldName, termFreqMap); } final TermsEnum termsEnum = vector.iterator(); final CharsRefBuilder spare = new CharsRefBuilder(); BytesRef text; while ((text = termsEnum.next()) != null) { spare.copyUTF8Bytes(text); final String term = spare.toString(); if (isNoiseWord(term)) { continue; } final int freq = (int) termsEnum.totalTermFreq(); // increment frequency Int cnt = termFreqMap.get(term); if (cnt == null) { cnt = new Int(); termFreqMap.put(term, cnt); cnt.x = freq; } else { cnt.x += freq; } } }
public void run() { for (int i = 0; i < 50; i++) { if (i % 3 == 0) { c.dec(); } else { c.inc(); } } }
@Test public void testFillSudokuGame() throws Exception { Random random = new Random(); moveThroughGame(game, (i, j) -> Int.fromInteger(random.nextInt(SIZE * SIZE) + 1)); assertThat(game.isFill(), is(true)); }
/** * Runs a query with an external variable declaration. * * @throws IOException I/O exception */ @Test public void queryBindSequence() throws IOException { Query query = session.query("declare variable $a external; $a"); query.bind("a", "1\u00012", "xs:integer"); assertEqual("1", query.next()); assertEqual("2", query.next()); query.close(); query = session.query("declare variable $a external; $a"); query.bind("a", "09\u0002xs:hexBinary\u00012", "xs:integer"); assertEqual("09", query.next()); assertEqual("2", query.next()); query.close(); query = session.query("declare variable $a external; $a"); query.bind("a", Seq.get(new Item[] {Int.get(1), Str.get("X")}, 2)); assertEqual("1", query.next()); assertEqual("X", query.next()); query.close(); query = session.query("declare variable $a external; $a"); query.bind("a", IntSeq.get(new long[] {1, 2}, AtomType.INT)); assertEqual("1", query.next()); assertEqual("2", query.next()); query.close(); query = session.query("declare variable $a external; $a"); query.bind("a", IntSeq.get(new long[] {1, 2}, AtomType.INT), "xs:integer"); assertEqual("1", query.next()); assertEqual("2", query.next()); query.close(); }
/** * Returns the nth geometry of a geometry collection, or the geometry if the input is not a * collection. * * @param node xml element containing gml object(s) * @param number integer number as the index of nth geometry * @return geometry as a gml element * @throws QueryException query exception */ @Deterministic public ANode geometryN(final ANode node, final Int number) throws QueryException { final Geometry geo = checkGeo(node); final long n = number.itr(); if (n < 1 || n > geo.getNumGeometries()) throw GeoErrors.outOfRangeIdx(number); return gmlWriter(geo.getGeometryN((int) n - 1)); }
RFView() { // hide in generated query page _oobee._hideInQuery = true; _numTrees._readOnly = true; _job._hideInQuery = true; _dest._hideInQuery = true; }
@Test public void testResolveCellBySquare() throws Exception { moveThroughGame(game, (i, j) -> Int.fromInteger((i * SIZE + i / SIZE + j) % (SIZE * SIZE) + 1)); game.set(9, 1, Int.ZERO); game.resolveBySquare(3, 1); assertThat(game.get(9, 1), is(Int.NINE)); }
/** * Returns the number of interior rings in a polygon. * * @param node xml element containing gml object(s) * @return integer number of interior rings * @throws QueryException query exception */ @Deterministic public Int numInteriorRing(final ANode node) throws QueryException { final Geometry geo = geo(node, Q_GML_POLYGON); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Polygon"); return Int.get(((Polygon) geo).getNumInteriorRing()); }
@Test public void testFindPossibleRowToResolve() throws Exception { moveThroughGame(game, (i, j) -> Int.fromInteger((i * SIZE + i / SIZE + j) % (SIZE * SIZE) + 1)); game.set(8, 1, Int.ZERO); game.set(9, 1, Int.ZERO); assertThat(game.possibleRowsToResolve(), containsInAnyOrder(8, 9)); }
@Override public Boolean visit(Int exp) { if (!exp.typeOf(environment).equals(new IntType())) { errorMsgs.add(String.format("Incorrect Int: %s", exp)); ; return false; } return true; }
@Override protected AbstractValue divInt(Int value) { if (this.value.compareTo(new BigDecimal(0)) == 0) { // Prevent division by zero. return new Money(this.value); } else { BigDecimal intAsBigDecimal = new BigDecimal(value.getValue()); return new Money(intAsBigDecimal.divide(this.value, Money.ROUNDING_MODE)); } }
/** * Returns the nth geometry of a geometry collection. * * @param node xml element containing gml object(s) * @param number index of i-th interior ring * @return n-th interior ring geometry (LineString) as a gml element * @throws QueryException query exception */ @Deterministic public ANode interiorRingN(final ANode node, final Int number) throws QueryException { final Geometry geo = geo(node, Q_GML_POLYGON); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Polygon"); final long n = number.itr(); final int max = ((Polygon) geo).getNumInteriorRing(); if (n < 1 || n > max) throw GeoErrors.outOfRangeIdx(number); return gmlWriter(((Polygon) geo).getInteriorRingN((int) n - 1)); }
/** * Returns the nth point of a line. * * @param node xml element containing gml object(s) * @param number index of i-th point * @return n-th point as a gml element * @throws QueryException query exception */ @Deterministic public ANode pointN(final ANode node, final Int number) throws QueryException { final Geometry geo = geo(node, Q_GML_LINEARRING, Q_GML_LINESTRING); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Line"); final int max = geo.getNumPoints(); final long n = number.itr(); if (n < 1 || n > max) throw GeoErrors.outOfRangeIdx(number); return gmlWriter(((LineString) geo).getPointN((int) n - 1)); }
@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { final byte[] s; if (exprs.length == 0) { final Item it = ctxValue(qc).item(qc, info); if (it instanceof FItem) throw FISTRING_X.get(ii, it.type); s = it == null ? Token.EMPTY : it.string(ii); } else { s = toEmptyToken(arg(0, qc), qc); } return Int.get(Token.length(s)); }
/** * Runs a query with an external variable declaration. * * @throws IOException I/O exception */ @Test public void queryBindInt() throws IOException { Query query = session.query("declare variable $a as xs:integer external; $a"); query.bind("a", "5", "xs:integer"); assertEqual("5", query.next()); query.close(); query = session.query("declare variable $a external; $a"); query.bind("a", Int.get(1), "xs:integer"); assertEqual("1", query.next()); query.close(); }
@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { checkCreate(qc); // URL to relational database final String url = string(toToken(exprs[0], qc)); final JDBCConnections jdbc = jdbc(qc); try { if (exprs.length > 2) { // credentials final String user = string(toToken(exprs[1], qc)); final String pass = string(toToken(exprs[2], qc)); if (exprs.length == 4) { // connection options final Options opts = toOptions(3, Q_OPTIONS, new Options(), qc); // extract auto-commit mode from options boolean ac = true; final HashMap<String, String> options = opts.free(); final String commit = options.get(AUTO_COMM); if (commit != null) { ac = Strings.yes(commit); options.remove(AUTO_COMM); } // connection properties final Properties props = connProps(options); props.setProperty(USER, user); props.setProperty(PASS, pass); // open connection final Connection conn = getConnection(url, props); // set auto/commit mode conn.setAutoCommit(ac); return Int.get(jdbc.add(conn)); } return Int.get(jdbc.add(getConnection(url, user, pass))); } return Int.get(jdbc.add(getConnection(url))); } catch (final SQLException ex) { throw BXSQ_ERROR_X.get(info, ex); } }
@SuppressWarnings("unchecked") @Test public void testIteratorRemove() throws Exception { Iterator iter = set.iterator(); try { iter.remove(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { } set.add(one); set.add(new Int(2)); set.add(new Int(3)); try { iter.remove(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { } dataService.setBinding("iter", new ManagedSerializable(iter)); newTransaction(); iter = (Iterator) dataService.getBinding("iter", ManagedSerializable.class).get(); while (iter.hasNext()) { Object next = iter.next(); if (one.equals(next)) { iter.remove(); try { iter.remove(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { } } } newTransaction(); iter = set.iterator(); int count = 0; while (iter.hasNext()) { assertFalse(one.equals(iter.next())); count++; } assertEquals(2, count); }
protected JsonObject defaultJsonResponse() { // This will be shown every request JsonObject r = new JsonObject(); RFModel model = _modelKey.value(); r.addProperty(DATA_KEY, _dataKey.originalValue()); r.addProperty(MODEL_KEY, _modelKey.originalValue()); r.addProperty(CLASS, _classCol.specified() ? _classCol.value() : findResponseIdx(model)); r.addProperty(NUM_TREES, model._totalTrees); r.addProperty(MTRY, model._splitFeatures); r.addProperty(MTRY_NODES, Arrays.toString(model._nodesSplitFeatures)); r.addProperty(OOBEE, _oobee.value()); // CM specific options r.addProperty(NO_CM, _noCM.value()); r.addProperty(JSON_REFRESH_THRESHOLD_CM, _refreshThresholdCM.value()); return r; }
@Override public boolean toHTML(StringBuilder sb) { DocGen.HTML.title(sb, "Confusion Matrix"); DocGen.HTML.arrayHead(sb); sb.append("<tr>"); sb.append("<th>Actual \\ Predicted</th>"); for (int i = 0; i < domain.length; i++) sb.append("<th>").append(domain[i]).append("</th>"); sb.append("<th>Error</th>"); sb.append("</tr>\n"); long tots[] = new long[cm.length]; long errs = 0, nrows = 0; for (int i = 0; i < domain.length; i++) { sb.append("<tr>"); sb.append("<th>").append(domain[i]).append("</th>"); long sum = 0; for (int j = 0; j < domain.length; j++) { sum += cm[i][j]; tots[j] += cm[i][j]; sb.append(i == j ? "<td style='background-color:LightGreen'>" : "<td>") .append(cm[i][j]) .append("</td>"); } long err = sum - cm[i][i]; errs += err; nrows += sum; sb.append(String.format("<td>%5.3f = %d / %d</td>", (double) err / sum, err, sum)); sb.append("</tr>\n"); } sb.append("<tr>"); sb.append("<th>Totals</th>"); for (int i = 0; i < domain.length; i++) sb.append("<td>").append(tots[i]).append("</td>"); sb.append(String.format("<th>%5.3f = %d / %d</th>", (double) errs / nrows, errs, nrows)); sb.append("</tr>\n"); DocGen.HTML.arrayTail(sb); DocGen.HTML.section(sb, "Summary"); DocGen.HTML.listHead(sb); DocGen.HTML.listBullet(sb, "ntrees", ntrees.value().toString(), 0); DocGen.HTML.listBullet(sb, "mtrys", Integer.toString(mtrys), 0); DocGen.HTML.listTail(sb); return true; }
/** * Constructor. * * @param tm time in milliseconds * @param ii input info * @throws QueryException query exception */ Dtm(final Int tm, final InputInfo ii) throws QueryException { this(Token.token(DATE.format(new java.util.Date(tm.itr(ii)))), ii); }
@Override public AbstractValue ltInt(Int value) { BigDecimal intAsBigDecimal = new BigDecimal(value.getValue()); return new Bool(intAsBigDecimal.compareTo(this.value) == -1); }
@Override protected AbstractValue subInt(Int value) { BigDecimal intAsBigDecimal = new BigDecimal(value.getValue()); return new Money(intAsBigDecimal.subtract(this.value)); }
/** * Returns the number of points in a geometry. * * @param node xml element containing gml object(s) * @return number of points int value * @throws QueryException query exception */ @Deterministic public Int numPoints(final ANode node) throws QueryException { return Int.get(checkGeo(node).getNumPoints()); }
/** * Returns a database function for the first node in a node set. * * @param n node set * @param i offset * @return function string */ static String openPre(final Nodes n, final int i) { return Function._DB_OPEN_PRE.get(Str.get(n.data.meta.name), Int.get(n.pres[i])).toString(); }
@Override protected AbstractValue mulInt(Int value) { BigDecimal intAsBigDecimal = new BigDecimal(value.getValue()); return new Money(intAsBigDecimal.multiply(this.value)); }
/** * Returns the dimension of an item. * * @param node xml element containing gml object(s) * @return dimension * @throws QueryException query exception */ @Deterministic public Int dimension(final ANode node) throws QueryException { return Int.get(checkGeo(node).getDimension()); }
public void run() { c.inc(); c.inc(); c.dec(); }