Skip to content

Commit

Permalink
Refactored PandasUtil#parsePandasCategorical(String) utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Dec 14, 2023
1 parent e0ed309 commit 1b96efe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pmml-lightgbm/src/main/java/org/jpmml/lightgbm/GBDT.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,12 @@ private List<List<?>> loadPandasCategorical(Section section){
String id = section.id();

try {
return PandasUtil.parsePandasCategorical(id);
List<List<?>> result = PandasUtil.parsePandasCategorical(id);
if(result == null){
result = Collections.emptyList();
}

return result;
} catch(Exception e){
throw new IllegalArgumentException(id, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jpmml.lightgbm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.Gson;
Expand Down Expand Up @@ -48,12 +47,7 @@ public List<List<?>> parsePandasCategorical(String string){
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();

List<List<?>> result = gson.fromJson(element, ListOfLists.class);
if(result == null){
result = Collections.emptyList();
}

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

static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
package org.jpmml.lightgbm;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class PandasUtilTest {

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

assertEquals(Collections.emptyList(), pandasCategories);
assertNull(pandasCategories);

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]]");

Expand Down

0 comments on commit 1b96efe

Please sign in to comment.