Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates in Python classes and 'assessModel' python phase #313

Merged
merged 5 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion client/src/main/java/zingg/client/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.scala.DefaultScalaModule;
import com.fasterxml.jackson.core.json.JsonWriteFeature;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -171,10 +173,33 @@ public static final Arguments createArgumentsFromJSON(String filePath, String ph
} catch (Exception e) {
//e.printStackTrace();
throw new ZinggClientException("Unable to parse the configuration at " + filePath +
". The error is " + e.getMessage());
". The error is " + e.getMessage(), e);
}
}

/**
* Write arguments to a json file
*
* @param filePath
* json file where arguments shall be written to
* @return Arguments object
* @throws ZinggClientException
* in case there is an error in writing to file
*/
public static final void writeArgumentsToJSON(String filePath, Arguments args)
throws ZinggClientException {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.getFactory().configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(),true);
LOG.warn("Arguments are written to file: " + filePath);
mapper.writeValue(new File(filePath), args);
} catch (Exception e) {
throw new ZinggClientException("Unable to write the configuration to " + filePath +
". The error is " + e.getMessage(), e);
}
}

public static void checkValid(Arguments args, String phase) throws ZinggClientException {
if (phase.equals("train") || phase.equals("match") || phase.equals("trainMatch") || phase.equals("link")) {
checkIsValid(args);
Expand Down Expand Up @@ -635,6 +660,7 @@ public void setBlockSize(long blockSize){
this.blockSize = blockSize;
}

@JsonIgnore
public String[] getPipeNames() {
Pipe[] input = this.getData();
String[] sourceNames = new String[input.length];
Expand Down
5 changes: 4 additions & 1 deletion client/src/main/java/zingg/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,8 @@ public Dataset<Row> getMarkedRecords() {
return zingg.getMarkedRecords();
}


public Dataset<Row> getUnMarkedRecords() {
return zingg.getUnMarkedRecords();
}

}
2 changes: 2 additions & 0 deletions client/src/main/java/zingg/client/IZingg.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public void init(Arguments args, String license)

public Dataset<Row> getMarkedRecords();

public Dataset<Row> getUnMarkedRecords();

public Long getMarkedRecordsStat(Dataset<Row> markedRecords, long value);

public Long getMatchedMarkedRecordsStat(Dataset<Row> markedRecords);
Expand Down
51 changes: 50 additions & 1 deletion client/src/test/java/zingg/client/TestArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -15,6 +16,9 @@
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;

import zingg.client.pipe.Format;
import zingg.client.pipe.Pipe;

public class TestArguments {

private static final String KEY_HEADER = "header";
Expand Down Expand Up @@ -241,4 +245,49 @@ public void testMatchTypeWrong() {


}
}

@Test
public void testWriteArgumentObjectToJSONFile() {
Arguments args = new Arguments();
try {
FieldDefinition fname = new FieldDefinition();
fname.setFieldName("fname");
fname.setDataType("\"string\"");
fname.setMatchType(Arrays.asList(MatchType.EXACT, MatchType.FUZZY, MatchType.PINCODE));
//fname.setMatchType(Arrays.asList(MatchType.EXACT));
fname.setFields("fname");
FieldDefinition lname = new FieldDefinition();
lname.setFieldName("lname");
lname.setDataType("\"string\"");
lname.setMatchType(Arrays.asList(MatchType.FUZZY));
lname.setFields("lname");
args.setFieldDefinition(Arrays.asList(fname, lname));

Pipe inputPipe = new Pipe();
inputPipe.setName("test");
inputPipe.setFormat(Format.CSV);
inputPipe.setProp("location", "examples/febrl/test.csv");
args.setData(new Pipe[] {inputPipe});

Pipe outputPipe = new Pipe();
outputPipe.setName("output");
outputPipe.setFormat(Format.CSV);
outputPipe.setProp("location", "examples/febrl/output.csv");
args.setOutput(new Pipe[] {outputPipe});

args.setBlockSize(400L);
args.setCollectMetrics(true);
args.setModelId("500");
Arguments.writeArgumentsToJSON("configFromArgObject.json", args);
//List<MatchType> fNameMatchType = args.getFieldDefinition().get(0).getMatchType();

// Arguments newArgs = Arguments.createArgumentsFromJSON("configFromArgObject.json", "test");
// assertEquals(newArgs.getModelId(), "500", "Model id is different");
// assertEquals(newArgs.getBlockSize(), 400L, "Block size is different");
// assertEquals(newArgs.getFieldDefinition().get(0).getFieldName(), "fname", "Field Definition[0]'s name is different");
//TODO add check for MatchType
} catch (Exception | ZinggClientException e) {
e.printStackTrace();
}
}
}
Loading