use of water.AutoBuffer in project h2o-2 by h2oai.
the class RequestBuilders method buildJSONResponseBox.
protected String buildJSONResponseBox(Response response) {
switch(response._status) {
case done:
RString result = new RString(_jsonResponseBox);
result.replace("JSON_RESPONSE_BOX", response._response == null ? new String(response._req.writeJSON(new AutoBuffer()).buf()) : GSON_BUILDER.toJson(response.toJson()));
return result.toString();
case error:
case redirect:
case poll:
default:
return "";
}
}
use of water.AutoBuffer in project h2o-3 by h2oai.
the class NetworkInitTest method testIPv4AddressEncoding.
// Test for H2OKey
@Test
public void testIPv4AddressEncoding() {
byte[] address = toByte(ari(10, 10, 1, 44));
int ipv4 = (int) ArrayUtils.encodeAsLong(address);
AutoBuffer ab = new AutoBuffer();
byte[] returnedAddress = ab.put4(ipv4).flipForReading().getA1(4);
Assert.assertArrayEquals(address, returnedAddress);
}
use of water.AutoBuffer in project h2o-3 by h2oai.
the class BufferedStringTest method testRead_impl.
@Ignore("This test fails currently - bugs in AutoBuffer, probably")
@Test
public void testRead_impl() throws Exception {
final String source = "this is not a string";
BufferedString sut1 = new BufferedString(source);
AutoBuffer ab = new AutoBuffer();
sut1.write_impl(ab);
ab.bufClose();
BufferedString sut2 = new BufferedString("what?");
sut2.read_impl(ab);
assertEquals(sut1, sut2);
}
use of water.AutoBuffer in project h2o-2 by h2oai.
the class VariableImportance method map.
@Override
public void map(Chunk[] chks) {
_votesOOB = new long[_ntrees];
_votesSOOB = new long[_ntrees];
_voteDiffs = new long[_ntrees];
_varimp = 0.f;
_varimpSD = 0.f;
_nrows = new long[_ntrees];
double[] data = new double[_ncols];
float[] preds = new float[_nclasses + 1];
final int rows = chks[0]._len;
int _N = _nclasses;
// shuffled oob rows
int[] soob = null;
boolean collectOOB = true;
final int cmin = _cmin;
//Need the chunk of code to score over every tree...
//Doesn't do anything with the first tree, we score time last *manually* (after looping over all da trees)
long seedForOob = ShuffleTask.seed(chks[0].cidx());
for (int ntree = 0; ntree < _ntrees; ntree++) {
int oobcnt = 0;
// oob rows
ArrayList<Integer> oob = new ArrayList<Integer>();
long treeSeed = _model.seed(ntree);
byte producerId = _model.producerId(ntree);
int init_row = (int) chks[0]._start;
long seed = Sampling.chunkSampleSeed(treeSeed, init_row);
Random rand = Utils.getDeterRNG(seed);
// Now for all rows, classify & vote!
for (int row = 0; row < rows; row++) {
// int row = r + (int)chks[0]._start;
// ------ THIS CODE is crucial and serve to replay the same sequence
// of random numbers as in the method Data.sampleFair()
// Skip row used during training if OOB is computed
float sampledItem = rand.nextFloat();
// Do not skip yet the rows with NAs in the rest of columns
if (chks[_ncols - 1].isNA0(row))
continue;
if (sampledItem < _model.sample)
continue;
oob.add(row);
oobcnt++;
// Predict with this tree - produce 0-based class index
int prediction = (int) _model.classify0(ntree, chks, row, _modelDataMap, (short) _N, false);
// Junk row cannot be predicted
if (prediction >= _nclasses)
continue;
// Check tree miss
int alignedPrediction = alignModelIdx(prediction);
int alignedData = alignDataIdx((int) chks[_classcol].at80(row) - cmin);
if (alignedPrediction == alignedData)
_votesOOB[ntree]++;
}
_oobs = new int[oob.size()];
for (int i = 0; i < oob.size(); ++i) _oobs[i] = oob.get(i);
//score on shuffled data...
if (soob == null || soob.length < oobcnt)
soob = new int[oobcnt];
// Shuffle array and copy results into <code>soob</code>
Utils.shuffleArray(_oobs, oobcnt, soob, seedForOob, 0);
for (int j = 0; j < oobcnt; j++) {
int row = _oobs[j];
// - prepare a row data
for (int i = 0; i < chks.length - 1; i++) {
// 1+i - one free is expected by prediction
data[i] = chks[i].at0(row);
}
// - permute variable
if (_var >= 0)
data[_var] = chks[_var].at0(soob[j]);
else
assert false;
// - score data
// - score only the tree
//.classify0(ntree, _data, chks, row, _modelDataMap, numClasses );
int prediction = (int) Tree.classify(new AutoBuffer(_model.tree(ntree)), data, (double) _N, false);
if (prediction >= _nclasses)
continue;
int pred = alignModelIdx(prediction);
int actu = alignDataIdx((int) chks[_classcol].at80(_oobs[j]) - cmin);
if (pred == actu)
_votesSOOB[ntree]++;
_nrows[ntree]++;
}
}
}
use of water.AutoBuffer in project h2o-3 by h2oai.
the class NetworkInitTest method testIPv6AddressEncoding.
// Test for H2OKey
@Test
public void testIPv6AddressEncoding() {
byte[] address = toByte(toOctects(ari(0x2001, 0xdb8, 0x1234, 0x0001, 0x9988, 0x7766, 0xdead, 0xbabe)));
long high = ArrayUtils.encodeAsLong(address, 8, 8);
long low = ArrayUtils.encodeAsLong(address, 0, 8);
AutoBuffer ab = new AutoBuffer();
byte[] returnedAddress = ab.put8(low).put8(high).flipForReading().getA1(16);
Assert.assertArrayEquals(address, returnedAddress);
}