public static PMML readPMMLFromUpdateKeyMessage(String key, String message, Configuration hadoopConf) throws IOException { String pmmlString; switch (key) { case "MODEL": pmmlString = message; break; case "MODEL-REF": // Allowing null is mostly for integration tests if (hadoopConf == null) { hadoopConf = new Configuration(); } Path messagePath = new Path(message); FileSystem fs = FileSystem.get(messagePath.toUri(), hadoopConf); try (InputStreamReader in = new InputStreamReader(fs.open(messagePath), StandardCharsets.UTF_8)) { pmmlString = CharStreams.toString(in); } catch (FileNotFoundException fnfe) { log.warn("Unable to load model file at {}; ignoring", messagePath); return null; } break; default: throw new IllegalArgumentException("Unknown key " + key); } return PMMLUtils.fromString(pmmlString); }
@Test public void testPreviousPMMLVersion() throws Exception { String pmml42 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<PMML xmlns=\"http://www.dmg.org/PMML-4_2\" version=\"4.2.1\">\n" + " <Header>\n" + " <Application name=\"Oryx\"/>\n" + " </Header>\n" + " <TreeModel functionName=\"classification\">\n" + " <Node recordCount=\"123.0\"/>\n" + " </TreeModel>\n" + "</PMML>\n"; PMML model = PMMLUtils.fromString(pmml42); // Actually transforms to latest version: assertEquals(PMMLUtils.VERSION, model.getVersion()); }
@Test public void testExtensionContent() throws Exception { PMML model = buildDummyModel(); assertNull(AppPMMLUtils.getExtensionContent(model, "foo")); AppPMMLUtils.addExtensionContent(model, "foo1", Arrays.asList("bar", "baz")); AppPMMLUtils.addExtensionContent(model, "foo2", Collections.emptyList()); AppPMMLUtils.addExtensionContent(model, "foo3", Arrays.asList(" c\" d \"e ", " c\" d \"e ")); PMML reserializedModel = PMMLUtils.fromString(PMMLUtils.toString(model)); assertEquals(Arrays.asList("bar", "baz"), AppPMMLUtils.getExtensionContent(reserializedModel, "foo1")); assertNull(AppPMMLUtils.getExtensionContent(reserializedModel, "foo2")); assertEquals(Arrays.asList(" c\" d \"e ", " c\" d \"e "), AppPMMLUtils.getExtensionContent(reserializedModel, "foo3")); }
PMML pmml = PMMLUtils.fromString(updates.get(0).getMessage()); CategoricalValueEncodings encodings = AppPMMLUtils.buildCategoricalValueEncodings(pmml.getDataDictionary());
assertEquals("MODEL", updates.get(0).getKey()); assertEquals(2, Integer.parseInt(AppPMMLUtils.getExtensionValue( PMMLUtils.fromString(updates.get(0).getMessage()), "features")));
assertEquals("MODEL", updates.get(0).getKey()); PMML pmml = PMMLUtils.fromString(updates.get(0).getMessage()); Model model = pmml.getModels().get(0); assertInstanceOf(model, ClusteringModel.class);
@Test public void testExtensionValue() throws Exception { PMML model = buildDummyModel(); assertNull(AppPMMLUtils.getExtensionValue(model, "foo")); AppPMMLUtils.addExtension(model, "foo", "bar"); PMML reserializedModel = PMMLUtils.fromString(PMMLUtils.toString(model)); assertEquals("bar", AppPMMLUtils.getExtensionValue(reserializedModel, "foo")); }
@Test public void testFromString() throws Exception { PMML model = buildDummyModel(); PMML model2 = PMMLUtils.fromString(PMMLUtils.toString(model)); assertEquals(model.getHeader().getApplication().getName(), model2.getHeader().getApplication().getName()); assertEquals(model.getModels().get(0).getMiningFunction(), model2.getModels().get(0).getMiningFunction()); }
public static PMML readPMMLFromUpdateKeyMessage(String key, String message, Configuration hadoopConf) throws IOException { String pmmlString; switch (key) { case "MODEL": pmmlString = message; break; case "MODEL-REF": // Allowing null is mostly for integration tests if (hadoopConf == null) { hadoopConf = new Configuration(); } Path messagePath = new Path(message); FileSystem fs = FileSystem.get(messagePath.toUri(), hadoopConf); try (InputStreamReader in = new InputStreamReader(fs.open(messagePath), StandardCharsets.UTF_8)) { pmmlString = CharStreams.toString(in); } catch (FileNotFoundException fnfe) { log.warn("Unable to load model file at {}; ignoring", messagePath); return null; } break; default: throw new IllegalArgumentException("Unknown key " + key); } return PMMLUtils.fromString(pmmlString); }