Skip to content

Commit c264253

Browse files
committed
Implemented Neural Network evaluation
1 parent 3c70808 commit c264253

File tree

11 files changed

+301
-21
lines changed

11 files changed

+301
-21
lines changed

logicaldoc-gui/src/main/java/com/logicaldoc/gui/common/client/grid/RunningListGridField.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.logicaldoc.gui.common.client.grid;
22

33
import com.logicaldoc.gui.common.client.util.AwesomeFactory;
4+
import com.smartgwt.client.types.Alignment;
45
import com.smartgwt.client.widgets.grid.ListGridRecord;
56

67
/**
@@ -15,16 +16,14 @@ public class RunningListGridField extends ColoredListGridField {
1516
private static final String RUNNING = "running";
1617

1718
public RunningListGridField() {
18-
super(RUNNING, " ", 30);
19-
setCanFilter(true);
20-
setCanSort(true);
21-
setCellFormatter((value, rec, rowNum, colNum) -> formatStatusIconCell(rec));
19+
this(RUNNING);
2220
}
23-
21+
2422
public RunningListGridField(String name) {
2523
super(name, " ", 30);
2624
setCanFilter(true);
2725
setCanSort(true);
26+
setAlign(Alignment.CENTER);
2827
setCellFormatter((value, rec, rowNum, colNum) -> formatStatusIconCell(rec));
2928
}
3029

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/ai/AIService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public interface AIService extends RemoteService {
8282
*/
8383
public GUIModel getModel(long modelId) throws ServerException;
8484

85-
8685
/**
8786
* Retrieves all the models
8887
*
@@ -91,7 +90,7 @@ public interface AIService extends RemoteService {
9190
* @throws ServerException an error happened in the server application
9291
*/
9392
public List<GUIModel> getModels() throws ServerException;
94-
93+
9594
/**
9695
* Trains a model
9796
*
@@ -101,6 +100,15 @@ public interface AIService extends RemoteService {
101100
*/
102101
public void trainModel(long modelId) throws ServerException;
103102

103+
/**
104+
* Evaluated a neural network model
105+
*
106+
* @param modelId identifier of the neural network model to evaluate
107+
*
108+
* @throws ServerException an error happened in the server application
109+
*/
110+
public void evaluateModel(long modelId) throws ServerException;
111+
104112
public static class Instance {
105113
private static AIServiceAsync inst;
106114

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/ai/AIServiceAsync.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public interface AIServiceAsync {
2323
void getModels(AsyncCallback<List<GUIModel>> callback);
2424

2525
void trainModel(long modelId, AsyncCallback<Void> callback);
26+
27+
void evaluateModel(long modelId, AsyncCallback<Void> callback);
2628
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.logicaldoc.gui.frontend.client.ai.model;
2+
3+
import java.io.Serializable;
4+
import java.util.Date;
5+
6+
/**
7+
* The result of an evaluation over a Neural Network to check the accuracy
8+
*
9+
* @author Marco Meschieri - LogicalDOC
10+
* @since 9.2
11+
*/
12+
public class GUIEvaluation implements Serializable {
13+
14+
private static final long serialVersionUID = 1L;
15+
16+
/**
17+
* Human readable description of the network performance
18+
*/
19+
private String report;
20+
21+
/**
22+
* HTML representation of the confusion matrix
23+
*/
24+
private String confusionMatrix;
25+
26+
private Date lastEvaluated;
27+
28+
/**
29+
* Indicates if there is a currently running evaluation process
30+
*/
31+
private boolean evaluating = false;
32+
33+
public GUIEvaluation() {
34+
super();
35+
}
36+
37+
public GUIEvaluation(String stats, String confusionMatrix) {
38+
super();
39+
this.report = stats;
40+
this.confusionMatrix = confusionMatrix;
41+
}
42+
43+
public String getReport() {
44+
return report;
45+
}
46+
47+
public void setReport(String report) {
48+
this.report = report;
49+
}
50+
51+
public String getConfusionMatrix() {
52+
return confusionMatrix;
53+
}
54+
55+
public void setConfusionMatrix(String confusionMatrix) {
56+
this.confusionMatrix = confusionMatrix;
57+
}
58+
59+
public Date getLastEvaluated() {
60+
return lastEvaluated;
61+
}
62+
63+
public void setLastEvaluated(Date lastEvaluated) {
64+
this.lastEvaluated = lastEvaluated;
65+
}
66+
67+
public boolean isEvaluating() {
68+
return evaluating;
69+
}
70+
71+
public void setEvaluating(boolean evaluating) {
72+
this.evaluating = evaluating;
73+
}
74+
}

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/ai/model/GUIModel.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public class GUIModel implements Serializable {
4040

4141
private long seed = 123;
4242

43-
private GUITraining training = new GUITraining();
44-
4543
private int cutoff = 1;
4644

4745
private int ngramMin = 2;
@@ -50,6 +48,10 @@ public class GUIModel implements Serializable {
5048

5149
private String language = "en";
5250

51+
private GUITraining training = new GUITraining();
52+
53+
private GUIEvaluation evaluation = new GUIEvaluation();
54+
5355
public GUIModel(long id, String name) {
5456
super();
5557
this.id = id;
@@ -207,4 +209,16 @@ public GUITraining getTraining() {
207209
public void setTraining(GUITraining training) {
208210
this.training = training;
209211
}
212+
213+
public GUIEvaluation getEvaluation() {
214+
return evaluation;
215+
}
216+
217+
public void setEvaluation(GUIEvaluation evaluation) {
218+
this.evaluation = evaluation;
219+
}
220+
221+
public boolean isNeuralNetwork() {
222+
return "neural".equals(type);
223+
}
210224
}

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/ai/model/ModelDS.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public ModelDS() {
2828
DataSourceTextField typeField = new DataSourceTextField("type");
2929
DataSourceDateTimeField trained = new DataSourceDateTimeField("trained");
3030
DataSourceBooleanField training = new DataSourceBooleanField("training");
31+
DataSourceDateTimeField evaluated = new DataSourceDateTimeField("evaluated");
32+
DataSourceBooleanField evaluation = new DataSourceBooleanField("evaluation");
3133

32-
setFields(id, name, label, training, trained, description, typeField);
34+
setFields(id, name, label, training, trained, description, typeField, evaluated, evaluation);
3335
setClientOnly(true);
3436

3537
setDataURL("data/ai.xml?object=model");

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/ai/model/ModelDetailsPanel.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* @since 9.2
1717
*/
1818
public class ModelDetailsPanel extends VLayout {
19+
private static final String EVALUATION = "evaluation";
20+
1921
private GUIModel model;
2022

2123
private Layout propertiesTabPanel;
@@ -26,6 +28,10 @@ public class ModelDetailsPanel extends VLayout {
2628

2729
private ModelTraining trainingPanel;
2830

31+
private Layout evaluationTabPanel;
32+
33+
private ModelEvaluation evaluationPanel;
34+
2935
private EditingTabSet tabSet;
3036

3137
private ModelsPanel modelsPanel;
@@ -61,7 +67,6 @@ public void onSuccess(GUIModel sampler) {
6167
propertiesTab.setPane(propertiesTabPanel);
6268
tabSet.addTab(propertiesTab);
6369

64-
6570
trainingTabPanel = new HLayout();
6671
trainingTabPanel.setWidth100();
6772
trainingTabPanel.setHeight100();
@@ -90,11 +95,24 @@ private void refresh() {
9095
propertiesTabPanel.removeMember(trainingPanel);
9196
}
9297

98+
if (evaluationPanel != null) {
99+
evaluationPanel.destroy();
100+
if (Boolean.TRUE.equals(evaluationTabPanel.contains(evaluationPanel)))
101+
evaluationTabPanel.removeMember(evaluationPanel);
102+
}
103+
93104
propertiesPanel = new ModelProperties(model, event -> onModified());
94105
propertiesTabPanel.addMember(propertiesPanel);
95106

96107
trainingPanel = new ModelTraining(model, event -> onModified());
97108
trainingTabPanel.addMember(trainingPanel);
109+
110+
if (model.isNeuralNetwork()) {
111+
evaluationPanel = new ModelEvaluation(model, event -> onModified());
112+
evaluationTabPanel.addMember(evaluationPanel);
113+
} else {
114+
tabSet.removeTab(EVALUATION);
115+
}
98116
}
99117

100118
public GUIModel getModel() {
@@ -103,6 +121,17 @@ public GUIModel getModel() {
103121

104122
public void setModel(GUIModel model) {
105123
this.model = model;
124+
125+
if (model.isNeuralNetwork()) {
126+
evaluationTabPanel = new HLayout();
127+
evaluationTabPanel.setWidth100();
128+
evaluationTabPanel.setHeight100();
129+
Tab evaluationTab = new Tab(I18N.message(EVALUATION));
130+
evaluationTab.setID(EVALUATION);
131+
evaluationTab.setPane(evaluationTabPanel);
132+
tabSet.addTab(evaluationTab);
133+
}
134+
106135
refresh();
107136
}
108137

@@ -114,11 +143,11 @@ private boolean validate() {
114143
boolean valid = propertiesPanel.validate();
115144
if (!valid)
116145
tabSet.selectTab(0);
117-
146+
118147
valid = trainingPanel.validate();
119148
if (!valid)
120149
tabSet.selectTab(1);
121-
150+
122151
return valid;
123152
}
124153

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.logicaldoc.gui.frontend.client.ai.model;
2+
3+
import com.logicaldoc.gui.common.client.i18n.I18N;
4+
import com.logicaldoc.gui.common.client.util.ItemFactory;
5+
import com.smartgwt.client.types.TitleOrientation;
6+
import com.smartgwt.client.widgets.form.DynamicForm;
7+
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
8+
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
9+
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
10+
import com.smartgwt.client.widgets.layout.HLayout;
11+
12+
/**
13+
* Shows model's evaluation panel.
14+
*
15+
* @author Marco Meschieri - LogicalDOC
16+
* @since 9.2
17+
*/
18+
public class ModelEvaluation extends ModelDetailsTab implements ModelObserver {
19+
20+
private DynamicForm form = new DynamicForm();
21+
22+
private TextAreaItem report;
23+
24+
private StaticTextItem confusionMatrix;
25+
26+
private HLayout container = new HLayout();
27+
28+
public ModelEvaluation(GUIModel model, final ChangedHandler changedHandler) {
29+
super(model, changedHandler);
30+
setWidth100();
31+
setHeight100();
32+
33+
setMembers(container);
34+
35+
ModelController.get().addObserver(this);
36+
37+
refresh();
38+
}
39+
40+
private void refresh() {
41+
if (Boolean.TRUE.equals(container.contains(form)))
42+
container.removeChild(form);
43+
44+
report = ItemFactory.newTextAreaItem("report", model.getEvaluation().getReport());
45+
report.setWidth("*");
46+
47+
confusionMatrix = ItemFactory.newStaticTextItem("confusionmatrix", model.getEvaluation().getConfusionMatrix());
48+
49+
onModelChanged(model);
50+
51+
form = new DynamicForm();
52+
form.setNumCols(2);
53+
form.setWidth100();
54+
form.setTitleOrientation(TitleOrientation.TOP);
55+
form.setItems(confusionMatrix, report);
56+
57+
container.setWidth100();
58+
container.setMembersMargin(3);
59+
container.addMember(form);
60+
}
61+
62+
boolean validate() {
63+
return true;
64+
}
65+
66+
@Override
67+
public boolean equals(Object other) {
68+
return super.equals(other);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return super.hashCode();
74+
}
75+
76+
@Override
77+
public void onModelChanged(GUIModel mdl) {
78+
if (this.model.getId() == mdl.getId() && mdl.isNeuralNetwork()) {
79+
report.setValue(mdl.getEvaluation().getReport());
80+
report.setTitle(mdl.getEvaluation().getLastEvaluated() != null
81+
? I18N.message("lastevaluatedon", I18N.formatDate(mdl.getEvaluation().getLastEvaluated()))
82+
: I18N.message("report"));
83+
confusionMatrix.setValue(mdl.getEvaluation().getConfusionMatrix());
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)