Skip to content

Commit

Permalink
Expanded commit 5b98083
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed May 29, 2024
1 parent b107cec commit ab7a5f7
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 9 deletions.
5 changes: 2 additions & 3 deletions pmml-biogeme/src/main/java/biogeme/expressions/Beta.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Arrays;
import java.util.Map;

import numpy.core.ScalarUtil;
import org.dmg.pmml.FieldRef;

public class Beta extends ElementaryExpression {
Expand All @@ -43,15 +42,15 @@ public FieldRef toPMML(){
return super.toPMML();
}

public Number getValue(Map<?, ?> betas){
public Number getValue(Map<String, ? extends Number> betas){
Integer status = getStatus();

switch(status){
case Beta.STATUS_ESTIMATE:
{
String name = getName();

Number value = (Number)ScalarUtil.decode(betas.get(name));
Number value = betas.get(name);
if(value == null){
throw new IllegalArgumentException(name);
}
Expand Down
56 changes: 56 additions & 0 deletions pmml-biogeme/src/main/java/biogeme/results/Results.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-Biogeme
*
* JPMML-Biogeme is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Biogeme is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Biogeme. If not, see <http://www.gnu.org/licenses/>.
*/
package biogeme.results;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.razorvine.pickle.objects.ClassDict;
import org.jpmml.python.HasArray;
import org.jpmml.python.PythonObject;

public class Results extends PythonObject {

public Results(String module, String name){
super(module, name);
}

public Map<String, Number> getBetas(){
ClassDict data = getData();

List<String> betaNames = (List)data.get("betaNames");
List<Number> betaValues = (List)((HasArray)data.get("betaValues")).getArrayContent();

Map<String, Number> result = new LinkedHashMap<>();

for(int i = 0; i < betaNames.size(); i++){
String betaName = betaNames.get(i);
Number betaValue = betaValues.get(i);

result.put(betaName, betaValue);
}

return result;
}

public ClassDict getData(){
return get("data", ClassDict.class);
}
}
11 changes: 7 additions & 4 deletions pmml-biogeme/src/main/java/org/jpmml/biogeme/Experiment.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import biogeme.expressions.Plus;
import biogeme.expressions.Times;
import biogeme.expressions.Variable;
import biogeme.results.Results;
import com.google.common.collect.Iterables;
import org.dmg.pmml.DataField;
import org.dmg.pmml.DataType;
Expand Down Expand Up @@ -67,11 +68,13 @@ public Experiment(){

public PMML encodePMML(BiogemeEncoder encoder){
Model model = getModel();
Map<?, ?> betas = getBetas();
Results results = getResults();

Map<?, ?> utility = model.getUtil();
Map<?, ?> availability = model.getAv();

Map<String, Number> betas = results.getBetas();

if(!Objects.equals(utility.keySet(), availability.keySet())){
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -136,12 +139,12 @@ public Model getModel(){
return get("model", Model.class);
}

public Map<?, ?> getBetas(){
return getDict("betas");
public Results getResults(){
return get("results", Results.class);
}

static
private org.dmg.pmml.Model encodeUtility(Object choice, Expression expression, Map<?, ?> betas, BiogemeEncoder encoder){
private org.dmg.pmml.Model encodeUtility(Object choice, Expression expression, Map<String, ? extends Number> betas, BiogemeEncoder encoder){
declareVariables(expression, encoder);

List<Feature> features = new ArrayList<>();
Expand Down
31 changes: 31 additions & 0 deletions pmml-biogeme/src/main/java/tomlkit/items/Float.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-Biogeme
*
* JPMML-Biogeme is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Biogeme is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Biogeme. If not, see <http://www.gnu.org/licenses/>.
*/
package tomlkit.items;

public class Float extends Item {

public Float(String module, String name){
super(module, name);
}

@Override
public Number getValue(){
return getNumber("value");
}
}
31 changes: 31 additions & 0 deletions pmml-biogeme/src/main/java/tomlkit/items/Integer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-Biogeme
*
* JPMML-Biogeme is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Biogeme is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Biogeme. If not, see <http://www.gnu.org/licenses/>.
*/
package tomlkit.items;

public class Integer extends Item {

public Integer(String module, String name){
super(module, name);
}

@Override
public Number getValue(){
return getNumber("value");
}
}
47 changes: 47 additions & 0 deletions pmml-biogeme/src/main/java/tomlkit/items/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-Biogeme
*
* JPMML-Biogeme is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Biogeme is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Biogeme. If not, see <http://www.gnu.org/licenses/>.
*/
package tomlkit.items;

import org.jpmml.python.CythonObject;

abstract
public class Item extends CythonObject {

public Item(String module, String name){
super(module, name);
}

abstract
public Object getValue();

@Override
public void __init__(Object[] args){
super.__setstate__(INIT_ARGS, args);
}

public String getRaw(){
return getString("raw");
}

private static final String[] INIT_ARGS = {
"value",
"trivia",
"raw"
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ biogeme.expressions.elementary_expressions.Beta = biogeme.expressions.Beta
biogeme.expressions.elementary_expressions.Variable = biogeme.expressions.Variable
biogeme.expressions.logit_expressions._bioLogLogitFullChoiceSet = biogeme.expressions.LogLogit
biogeme.expressions.numeric_expressions.Numeric = biogeme.expressions.Numeric
biogeme.results.bioResults = biogeme.results.Results
tomlkit.items.Float =
tomlkit.items.Integer =
6 changes: 4 additions & 2 deletions pmml-biogeme/src/test/resources/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ def estimate(model_func):

return {
"model" : model,
"betas" : results.getBetaValues()
"results" : results
}

def predict(proba_func, experiment):
betas = experiment["results"].getBetaValues()

prediction = DataFrame()

choices = list(V.keys())

for choice in choices:
proba = proba_func(choice)
prediction["probability({})".format(choice)] = proba.getValue_c(betas = experiment["betas"], database = database, prepareIds = True)
prediction["probability({})".format(choice)] = proba.getValue_c(betas = betas, database = database, prepareIds = True)

prediction["Choice"] = [choices[idx] for idx in numpy.argmax(prediction.values, axis = 1)]

Expand Down
Binary file modified pmml-biogeme/src/test/resources/pkl/MNLAvOptima.pkl
Binary file not shown.
Binary file modified pmml-biogeme/src/test/resources/pkl/MNLOptima.pkl
Binary file not shown.

0 comments on commit ab7a5f7

Please sign in to comment.