use of ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel in project shifu by ShifuML.
the class IndependentTreeModelUtils method convertZipSpecToBinary.
public boolean convertZipSpecToBinary(File zipSpecFile, File outputGbtFile) {
ZipInputStream zipInputStream = null;
FileOutputStream fos = null;
try {
zipInputStream = new ZipInputStream(new FileInputStream(zipSpecFile));
IndependentTreeModel treeModel = null;
List<List<TreeNode>> trees = null;
ZipEntry zipEntry = null;
do {
zipEntry = zipInputStream.getNextEntry();
if (zipEntry != null) {
if (zipEntry.getName().equals(MODEL_CONF)) {
ByteArrayOutputStream byos = new ByteArrayOutputStream();
IOUtils.copy(zipInputStream, byos);
treeModel = JSONUtils.readValue(new ByteArrayInputStream(byos.toByteArray()), IndependentTreeModel.class);
} else if (zipEntry.getName().equals(MODEL_TREES)) {
DataInputStream dataInputStream = new DataInputStream(zipInputStream);
int size = dataInputStream.readInt();
trees = new ArrayList<List<TreeNode>>(size);
for (int i = 0; i < size; i++) {
int forestSize = dataInputStream.readInt();
List<TreeNode> forest = new ArrayList<TreeNode>(forestSize);
for (int j = 0; j < forestSize; j++) {
TreeNode treeNode = new TreeNode();
treeNode.readFields(dataInputStream);
forest.add(treeNode);
}
trees.add(forest);
}
}
}
} while (zipEntry != null);
if (treeModel != null && CollectionUtils.isNotEmpty(trees)) {
treeModel.setTrees(trees);
fos = new FileOutputStream(outputGbtFile);
treeModel.saveToInputStream(fos);
} else {
return false;
}
} catch (IOException e) {
logger.error("Error occurred when convert the zip format model to binary.", e);
return false;
} finally {
IOUtils.closeQuietly(zipInputStream);
IOUtils.closeQuietly(fos);
}
return true;
}
use of ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel in project shifu by ShifuML.
the class IndependentTreeModelUtils method convertBinaryToZipSpec.
public boolean convertBinaryToZipSpec(File treeModelFile, File outputZipFile) {
FileInputStream treeModelInputStream = null;
ZipOutputStream zipOutputStream = null;
try {
treeModelInputStream = new FileInputStream(treeModelFile);
IndependentTreeModel treeModel = IndependentTreeModel.loadFromStream(treeModelInputStream);
List<List<TreeNode>> trees = treeModel.getTrees();
treeModel.setTrees(null);
if (CollectionUtils.isEmpty(trees)) {
logger.error("No trees found in the tree model.");
return false;
}
zipOutputStream = new ZipOutputStream(new FileOutputStream(outputZipFile));
ZipEntry modelEntry = new ZipEntry(MODEL_CONF);
zipOutputStream.putNextEntry(modelEntry);
ByteArrayOutputStream byos = new ByteArrayOutputStream();
JSONUtils.writeValue(new OutputStreamWriter(byos), treeModel);
zipOutputStream.write(byos.toByteArray());
IOUtils.closeQuietly(byos);
ZipEntry treesEntry = new ZipEntry(MODEL_TREES);
zipOutputStream.putNextEntry(treesEntry);
DataOutputStream dataOutputStream = new DataOutputStream(zipOutputStream);
dataOutputStream.writeInt(trees.size());
for (List<TreeNode> forest : trees) {
dataOutputStream.writeInt(forest.size());
for (TreeNode treeNode : forest) {
treeNode.write(dataOutputStream);
}
}
IOUtils.closeQuietly(dataOutputStream);
} catch (IOException e) {
logger.error("Error occurred when convert the tree model to zip format.", e);
return false;
} finally {
IOUtils.closeQuietly(zipOutputStream);
IOUtils.closeQuietly(treeModelInputStream);
}
return true;
}
use of ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel in project shifu by ShifuML.
the class IndependentTreeModelTest method testGBTTreeEncode.
// @Test
public void testGBTTreeEncode() throws IOException {
IndependentTreeModel treeModel = IndependentTreeModel.loadFromStream(IndependentTreeModelTest.class.getResourceAsStream("/example/encode/model0.gbt"));
CsvFile csvFile = new CsvFile("src/test/resources/example/encode/sample.data.10", "\u0007");
for (Map<String, String> rawData : csvFile) {
Map<String, Object> input = new HashMap<String, Object>();
input.putAll(rawData);
List<String> instanceCodes = treeModel.encode(5, input);
System.out.println(instanceCodes);
}
}
use of ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel in project shifu by ShifuML.
the class IndependentTreeModelUtilsTest method testConvertZipToBinary.
@Test
public void testConvertZipToBinary() throws IOException {
File input = new File("src/test/resources/example/readablespec/model0.zip");
File output = new File("src/test/resources/example/readablespec/model1.gbt");
IndependentTreeModelUtils utils = new IndependentTreeModelUtils();
utils.convertZipSpecToBinary(input, output);
FileInputStream inputStream = new FileInputStream(output);
IndependentTreeModel treeModel = IndependentTreeModel.loadFromStream(inputStream);
Assert.assertTrue(treeModel != null);
FileUtils.deleteQuietly(output);
}
use of ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel in project shifu by ShifuML.
the class IndependentTreeModelTest method testGBTTree.
// @Test
public void testGBTTree() throws IOException {
IndependentTreeModel treeModel = IndependentTreeModel.loadFromStream(IndependentTreeModelTest.class.getResourceAsStream("/example/encode/model0.gbt"));
System.out.println(treeModel.getTrees().get(0).size());
}
Aggregations