Skip to content

Commit

Permalink
Added PandasUtil#formatPandasCategorical(List<List<?>>) utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Dec 14, 2023
1 parent 1b96efe commit d1a244d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
20 changes: 17 additions & 3 deletions pmml-lightgbm/src/main/java/org/jpmml/lightgbm/PandasUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,27 @@ public List<List<?>> parsePandasCategorical(String string){

JsonElement element = JsonParser.parseString(string);

Gson gson = new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();
Gson gson = createGson();

return gson.fromJson(element, ListOfLists.class);
}

static
public String formatPandasCategorical(List<List<?>> objects){
Gson gson = createGson();

return PandasUtil.PREFIX_PANDAS_CATEGORICAL + gson.toJson(objects, ListOfLists.class);
}

static
private Gson createGson(){
Gson result = new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();

return result;
}

static
private class ListOfLists extends ArrayList<List<?>> {
}
Expand Down
28 changes: 23 additions & 5 deletions pmml-lightgbm/src/test/java/org/jpmml/lightgbm/PandasUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,36 @@
public class PandasUtilTest {

@Test
public void parse() throws Exception {
List<List<?>> pandasCategories = parsePandasCategorical("null");
public void parse(){
assertNull(parsePandasCategorical("null"));

assertNull(pandasCategories);
assertEquals(PandasUtilTest.CATEGORIES_LIST_OF_LISTS, parsePandasCategorical(PandasUtilTest.CATEGORIES_STRING));
}

pandasCategories = parsePandasCategorical("[[\"null\", \"A\", \"B, B\", \"C, [C], C\"], [-2, -1, 0, 1, 2], [-2.0, -1.0, 0.0, 1.0, 2.0], [false, true]]");
@Test
public void format(){
assertEquals("null", formatPandasCategorical(null));

assertEquals(Arrays.asList(Arrays.asList("null", "A", "B, B", "C, [C], C"), Arrays.asList(-2L, -1L, 0L, 1L, 2L), Arrays.asList(-2d, -1d, 0d, 1d, 2d), Arrays.asList(Boolean.FALSE, Boolean.TRUE)), pandasCategories);
assertEquals(removeWhitespace(PandasUtilTest.CATEGORIES_STRING), removeWhitespace(formatPandasCategorical(PandasUtilTest.CATEGORIES_LIST_OF_LISTS)));
}

static
private List<List<?>> parsePandasCategorical(String value){
return PandasUtil.parsePandasCategorical(PandasUtil.PREFIX_PANDAS_CATEGORICAL + value);
}

static
private String formatPandasCategorical(List<List<?>> objects){
String result = PandasUtil.formatPandasCategorical(objects);

return result.substring(PandasUtil.PREFIX_PANDAS_CATEGORICAL.length());
}

static
private String removeWhitespace(String string){
return string.replaceAll("\\s","");
}

private static final String CATEGORIES_STRING = "[[\"null\", \"A\", \"B, B\", \"C, [C], C\"], [-2, -1, 0, 1, 2], [-2.0, -1.0, 0.0, 1.0, 2.0], [false, true]]";
private static final List<List<?>> CATEGORIES_LIST_OF_LISTS = Arrays.asList(Arrays.asList("null", "A", "B, B", "C, [C], C"), Arrays.asList(-2L, -1L, 0L, 1L, 2L), Arrays.asList(-2d, -1d, 0d, 1d, 2d), Arrays.asList(Boolean.FALSE, Boolean.TRUE));
}

0 comments on commit d1a244d

Please sign in to comment.