Search in sources :

Example 1 with IndependentTreeModel

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;
}
Also used : IndependentTreeModel(ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ZipInputStream(java.util.zip.ZipInputStream) TreeNode(ml.shifu.shifu.core.dtrain.dt.TreeNode) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with IndependentTreeModel

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;
}
Also used : IndependentTreeModel(ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel) ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) TreeNode(ml.shifu.shifu.core.dtrain.dt.TreeNode) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with IndependentTreeModel

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);
    }
}
Also used : HashMap(java.util.HashMap) IndependentTreeModel(ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel) CsvFile(ml.shifu.shifu.combo.CsvFile)

Example 4 with IndependentTreeModel

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);
}
Also used : IndependentTreeModel(ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 5 with IndependentTreeModel

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());
}
Also used : IndependentTreeModel(ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel)

Aggregations

IndependentTreeModel (ml.shifu.shifu.core.dtrain.dt.IndependentTreeModel)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ZipEntry (java.util.zip.ZipEntry)2 TreeNode (ml.shifu.shifu.core.dtrain.dt.TreeNode)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 CsvFile (ml.shifu.shifu.combo.CsvFile)1 Test (org.testng.annotations.Test)1