Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions h2o-core/src/main/java/water/fvec/NewChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,34 @@ protected final boolean isCategorical(int idx) {
}

public void addCategorical(int e) {
if(_ms == null || _ms.len() == _sparseLen)
append2slow();
if( e != 0 || !isSparseZero() ) {
_ms.set(_sparseLen,e);
if (_ms == null || _ms.len() == _sparseLen) {
if (2 * _sparseLen > _len && isSparseZero()) {
int ids[] = new int[_id.length + 1];
System.arraycopy(_id, 0, ids, 1, _id.length);
ids[0] = -1;
// append2slow() will run cancel_sparse which will lose the information about 0 being a category.
// To be safe just set to categorical only those zeros that are surrounded by categories.
// We care only about those zeros set before cancel_sparse was called after the call all zeros
// should be set correctly as category since isSparseZero() == false.
append2slow();
for (int i = 0; i < ids.length - 1; i++) {
if (_xs.isCategorical(ids[i + 1]) && (i == 0 || _xs.isCategorical(ids[i])))
for (int j = ids[i] + 1; j < ids[i + 1]; j++) {
_xs.setCategorical(j);
}
}
} else
append2slow();
Comment thread
tomasfryda marked this conversation as resolved.
}
if (e != 0 || !isSparseZero()) {
_ms.set(_sparseLen, e);
_xs.setCategorical(_sparseLen);
if(_id != null) _id[_sparseLen] = _len;
if (_id != null) _id[_sparseLen] = _len;
++_sparseLen;
}
++_len;
}

public void addNA() {
if(!_sparseNA) {
if (isString()) {
Expand Down
30 changes: 30 additions & 0 deletions h2o-core/src/test/java/water/fvec/NewChunkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,5 +629,35 @@ public void testSparseWithMissing() {
nc.addNumDecompose(Double.MIN_VALUE);
nc.addNumDecompose(Double.MIN_NORMAL);
}

@Test
public void testNewChunkSparseCategoricals() {
// this is reproduction of issue that caused failure in multinode for the
// targetencoding.interaction.InteractionSupportTest.test_addFeatureInteraction_with_known_domain_is_consistent
// test
int[] data = new int[96];
for (int i = 0; i < 19; i++)
data[i] = 0;
for (int i = 19; i < 62; i++)
data[i] = 2;
for (int i = 62; i < 86; i++)
data[i] = 7;
for (int i = 86; i < 90; i++)
data[i] = 12;
for (int i = 90; i < 94; i++)
data[i] = 0;
data[94] = 11;
data[95] = 11;

nc = new NewChunk(av, 25);
for (int i = 0; i < data.length; i++) {
nc.addCategorical(data[i]);
}
assertEquals(Vec.T_CAT, nc.type());
Chunk c = nc.new_close();
for (int i = 0; i < data.length; i++) {
assertEquals(data[i], (int)c.atd(i));
}
}
}

Loading