Castor Mapping Arrays - java

I've got a class that has constructor:
public ContEmpirical(double xs[], double fs[]) {
Check.check(xs.length == fs.length + 1 && fs.length > 0,
"Empirical distribution array mismatch");
this.xs = new double[xs.length];
this.xs = xs;
this.cs = new double[xs.length];
double fTotal = 0.0;
for (int i = 0; i < fs.length; i++)
fTotal += fs[i];
cs[0] = 0;
for (int i = 0; i < fs.length; i++)
cs[i + 1] = cs[i] + fs[i] / fTotal;
}
Attributes:
private double xs[], cs[];
private double fs[]; // this attribute i added to make castors life easier since it always wants to map constructor arg to class attribute.
The mapping File i have is:
<class name="tools.ContEmpirical">
<description xmlns="">
Mapping for class tools.ContEmpirical
</description>
<map-to xml="ContEmpirical"/>
<field name="xs" type="double" collection="array" set-method="%1" get-method="getXs" required="true">
<bind-xml node="attribute"/>
</field>
<field name="fs" type="double" collection="array" set-method="%2" get-method="getFs" required="true">
<bind-xml node="attribute"/>
</field></class>
Yet when i try to marshall an instance of ContEmpirical I get this XML:
<ContEmpirical xs="0.2 0.3 0.4 0.8"/>
When really I should be getting something like this:
<ContEmpirical xs="0.2 0.3 0.4 0.8" fs="0.2 0.3 0.4 0.8"/>
Is there something I'm missing from the mapping?

Can you post the ContEmpirical class? Are you initialising the fs array to something in the field level?
UPDATE: I must be missing something. You said you "expect" fs in the output xml. But does the class have an attribute named fs? From the constructor code you have posted, fs is never assigned internally (there is a parameter called fs though which is used for computing cs).
So really, your object has only a xs and cs variable and in your xml if you declare mapping for cs you should get cs.

Related

How to export Weka model made in Java to PMML format

Is there a way i can export a LinearRegression model ( build on some dataset) into a PMML format in Java?
The code so far
DataSource source = new DataSource("house.arff");
Instances dataset = source.getDataSet();
Instances m_structure = new Instances(dataset, 0);
m_structure.setClassIndex(dataset.numAttributes()-1);
dataset.setClassIndex(dataset.numAttributes()-1);
LinearRegression lReg = new LinearRegression();
int m_NumClasses = dataset.numClasses();
int class_index= dataset.classIndex();
int nK = m_NumClasses - 1;
int nR = dataset.numAttributes() - 1;
double[][] m_Par = new double[nR + 1][nK];
String pmmlx= LogisticProducerHelper.toPMML(dataset,m_structure,m_Par,m_NumClasses);
System.out.println(pmmlx);
This produces the following PMML file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PMML version="4.1" xmlns="http://www.dmg.org/PMML-4_1">
<Header copyright="WEKA">
<Application name="WEKA" version="3.8.0"/>
</Header>
<DataDictionary>
<DataField name="houseSize" optype="continuous"/>
<DataField name="lotSize" optype="continuous"/>
<DataField name="bedrooms" optype="continuous"/>
<DataField name="granite" optype="continuous"/>
<DataField name="bathroom" optype="continuous"/>
<DataField name="sellingPrice" optype="continuous"/>
</DataDictionary>
<RegressionModel algorithmName="logisticRegression" functionName="classification" modelType="logisticRegression" normalizationMethod="softmax">
<MiningSchema>
<MiningField missingValueReplacement="3132.0" missingValueTreatment="asMean" name="houseSize" usageType="active"/>
<MiningField missingValueReplacement="11788.142857142857" missingValueTreatment="asMean" name="lotSize" usageType="active"/>
<MiningField missingValueReplacement="5.0" missingValueTreatment="asMean" name="bedrooms" usageType="active"/>
<MiningField missingValueReplacement="0.42857142857142855" missingValueTreatment="asMean" name="granite" usageType="active"/>
<MiningField missingValueReplacement="0.7142857142857143" missingValueTreatment="asMean" name="bathroom" usageType="active"/>
<MiningField name="sellingPrice" usageType="predicted"/>
</MiningSchema>
<Output/>
</RegressionModel>
</PMML>
The PMML file above cannot be used to predict an Instance because the model is not yet built.
Using the following line builds the Classifier.
lReg.buildClassifier(dataset);
So I am wondering is there a way that I can add the parameters learned by this classifier into the PMML file so it can be exported/imported easily as a already trained classifier?
According to JavaDoc of LogisticProducerHelper:
Helper class for producing PMML for a Logistic classifier. Not designed to be used directly - you should call toPMML() on a trained Logistic classifier.
JavaDoc states that only Logistic classifier implements PMMLProducer.
If you use Logistic you can use logistic.toPMML(train) method.

Tensorflow in Android: How do i use my linear regression model to predict a value in an android application?

I currently have a ipynb file (ipython notebook) that contains a linear regression code / model(im not entirely sure if it's a model) that I've created earlier.
How do i implement this model in an android application such that if I were to input a value of 'x' in a text box, it'll output in a textview the predicted value of 'y'. Function: Y = mx + b.
I've tried looking at different tutorials, but they were mostly not "step-by-step" guides, which made it really hard to understand, I'm a beginner at coding.
Here's my code for the model:
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
rng = np.random
from numpy import genfromtxt
from sklearn.datasets import load_boston
# Parameters
learning_rate = 0.0001
training_epochs = 1000
display_step = 50
n_samples = 222
X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")
filename_queue = tf.train.string_input_producer(["C://Users//Shiina//battdata.csv"],shuffle=False)
reader = tf.TextLineReader() # skip_header_lines=1 if csv has headers
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1.], [1.]]
height, soc= tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.stack([height])
# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
# Construct a linear model
pred_soc = tf.add(tf.multiply(height, W), b) # XW + b <- y = mx + b where W is gradient, b is intercept
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred_soc-soc, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initializing the variables
init = tf.global_variables_initializer()
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
_, cost_value = sess.run([optimizer,cost])
#Display logs per epoch step
if (epoch+1) % display_step == 0:
c = sess.run(cost)
print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
"W=", sess.run(W), "b=", sess.run(b))
print("Optimization Finished!")
training_cost = sess.run(cost)
print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
#Plot data after completing training
train_X = []
train_Y = []
for i in range(n_samples): #Your input data size to loop through once
X, Y = sess.run([height, pred_soc]) # Call pred, to get the prediction with the updated weights
train_X.append(X)
train_Y.append(Y)
#Graphic display
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.ylabel("SoC")
plt.xlabel("Height")
plt.axis([0, 1, 0, 100])
plt.plot(train_X, train_Y, linewidth=2.0)
plt.legend()
plt.show()
coord.request_stop()
coord.join(threads)

Solr Custom Transfomer Not Working?

I am trying to Add Few Fields to solr Index While indexing with Data Import Handler.
Below is My data-config.xml
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db" user="*****" password="*********"/>
<script><![CDATA[
function addMergedPdt(row)
{
var m = row.get('mergedPdt');
if(m == null)
{
row.put('mergedPdt',9999999999);
}
return row;
}
]]></script>
<script><![CDATA[
function transform(row)
{
if(row.get(mergedPdt) == null)
{
row.put('catStock', 0);
row.put('catPxMrp',0);
row.put('catPrice',0);
row.put('catCount',1)
row.put('catRating',0);
row.put('catAval',0);
return row;
}
else
{
row.put('catAval',1);
return row;
}
}
]]></script>
<document>
<entity name="product" onError="continue" transformer="script:addMergedPdt" query="select p.id, name, image, stock, lower(p.seller) as seller, brand,
cast(price as signed) as price, cast(pxMrp as signed) as pxMrp, mergedPdt, shipDays, url, cast(rating as signed) as rating,
disc(price, pxMrp) as discount, mc.node as seller_cat, oc.node as cat, substring_index(oc.node, '|', 1) as cat1,
substring(substring_index(oc.node, '|', 2), length(substring_index(oc.node, '|', 1)) + 2) as cat2,
substring(substring_index(oc.node, '|', 3), length(substring_index(oc.node, '|', 2)) + 2) as cat3
from _products as p, _mergedCat as mc, _ourCat as oc where active = 1 and cat_id = mc.id and ourCat = oc.id and
('${dataimporter.request.full}' != 'false' OR last_visited > '${dataimporter.last_index_time}') limit 10000">
<!-- To Papulate the Catalog Data -->
<entity name="mergedPdt" transformer="script:transform" onError="continue" query = "SELECT mergedPdt,count(*) as catCount,cast(max(stock) as signed) as catStock,cast(max(pxMrp) as signed) as catPxMrp,cast(min(price) as signed) as catPrice,cast(avg(rating) as signed) as catRating FROM `_products` where mergedPdt = ${product.mergedPdt}"/>
</entity>
I am Getting some Error Like
org.apache.solr.handler.dataimport.DataImportHandlerException: Error invoking script for entity mergedPdt Processing Document # 10000
at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:70)
at org.apache.solr.handler.dataimport.ScriptTransformer.transformRow(ScriptTransformer.java:59)
at org.apache.solr.handler.dataimport.EntityProcessorWrapper.applyTransformer(EntityProcessorWrapper.java:198)
at org.apache.solr.handler.dataimport.EntityProcessorWrapper.nextRow(EntityProcessorWrapper.java:256)
at org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:475)
at org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:514)
at org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:414)
at org.apache.solr.handler.dataimport.DocBuilder.doFullDump(DocBuilder.java:329)
at org.apache.solr.handler.dataimport.DocBuilder.execute(DocBuilder.java:232)
at org.apache.solr.handler.dataimport.DataImporter.doFullImport(DataImporter.java:416)
at org.apache.solr.handler.dataimport.DataImporter.runCmd(DataImporter.java:480)
at org.apache.solr.handler.dataimport.DataImporter$1.run(DataImporter.java:461)
Caused by: java.lang.NoSuchMethodException: no such method: transform
at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:286)
at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:258)
at org.apache.solr.handler.dataimport.ScriptTransformer.transformRow(ScriptTransformer.java:55)
... 10 more
and all fields are being Indexed excluding the extra fields which i tried adding using the transformer.
And surprisingly only one field "catCount" has been indexed.
You can trust me and i am confident regarding the schema definition and other configurations.
any lead will be higly appriciated ??
Thanks in advance :)

Problems integrating Matlab with Java for deployment in web

I am seriously new to Matlab and Matlab Builder JA. I am trying to make a simple Java web by calling a Matlab functions. So, I have model.m file. I have put in the functions to be used as a library to run in a server and have some variables to be input by users, which are 'Time' (eg. 20) and 'Dose' (eg. 1 2 0 0). I have tried to integrate all the codes and include the Matlab library. But unfortunately, only the .html file can be run. Below are my codes and error that I get. Hope someone could help me to check whether I am compiling the model.m file correctly and the Java file is coded also correct. For your information, I am using Eclipse Kepler and Tomcat 7.0.54. Thanks in advance!
First of all, this is the error that I got when I run the application.
EDITED
HTTP Status 500-
java.lang.NullPointerException
com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method)
com.mathworks.toolbox.javabuilder.internal.MWMCR.access$600(MWMCR.java:23)
com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mclFeval(MWMCR.java:833)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:731)
com.sun.proxy.$Proxy11.mclFeval(Unknown Source)
com.mathworks.toolbox.javabuilder.internal.MWMCR.invoke(MWMCR.java:406)
runPKmodelV1.Function.runPKmodelV1(Function.java:217)
SecondServlet.doGet(SecondServlet.java:78)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
This is the model.m file that have all the functions needed, with an output figure.
MATLAB model.m
function w = model(tf_sim,y0)
%%--------------------------------------------------------------------
% Running ODE model - Initial Conditions and setting up ode solver
%---------------------------------------------------------------------
%Initial Conditions:
%Initial Conditions:
% Dose_Central = 1;
% Drug_Central = 0;
% Dose_Peripheral = 0;
% Drug_Peripheral = 0;
%tf_sim=10; %Simulation time
% y0(1) = 1; % Dose in Central Compartment
% y0(2) = 0; % Drug in Central Compartment
% y0(3) = 0; % Dose in Peripheral Compartment
% y0(4) = 0; % Drug in Peripheral Compartment
options = odeset('RelTol',1e-4,'AbsTol',1e-5);
%%% Solving ODEs, final time = 2e+9 sec (aprox. 63 years) %%%
[t,y]=ode15s(#PK_Model_v1,[0 tf_sim],y0,options);
%%% Plotting state variables %%%
f = figure;
subplot(2,2,1);
plot(t,y(:,1),'r-');
subplot(2,2,2);
plot(t,y(:,2),'b-');
subplot(2,2,3);
plot(t,y(:,3),'m-');
subplot(2,2,4);
plot(t,y(:,4),'k-');
w = webfigure(f);
close(f);
end
function dy = PK_Model_v1(time,y)
%Parameter Values:
Tk0_Central = 1;
TLag_Central = 1;
Km_Central = 1;
Vm_Central = 30;
Tk0_Peripheral = 1;
TLag_Peripheral = 1;
Q12 = 1;
k12 = 1;
k21 = 1;
Central = 1;
Peripheral = 1;
ka_Central = 0.5;
ka_Peripheral = 0.1;
%Fluxes:
ReactionFlux1 = ka_Central*y(1);
ReactionFlux2 = Vm_Central*y(2)/(Km_Central+y(2));
ReactionFlux3 = ka_Peripheral*y(3);
ReactionFlux4 = (k12*y(2))*Central-(k21*y(4))*Peripheral;
dy(1,1) = -ReactionFlux1;
dy(2,1) = 1/Central*(ReactionFlux1 - ReactionFlux2 - ReactionFlux4);
dy(3,1) = -ReactionFlux3;
dy(4,1) = 1/Peripheral*(ReactionFlux3 + ReactionFlux4);
end
This is the welcome page, with all the variables to be input by users.
Eclipse page.html
<form action="SecondServlet">
<p> </p>
<p>PK Model Example</p>
<p> </p>
<p>Time</p>
<input type="text" name="tf_sim" value="" />
<p>Dosage</p>
<input type="text" name="y0" value="" />
<!-- Submit -->
<input type="submit" value="Display" name="DoPLot" />
<p> </p>
</form>
There is nothing in the index.jsp page. Only the webfigure after the submit button from page.html is clicked.
Eclipse index.jsp
<div ALIGN="CENTER">
<wf:web-figure root="WebFigures" name="Project_Figure" scope="session" />
</div>
This consists of codes used to get the data keyed in by the users and finally calculate the result with the use of Matlab library and dispatch the result into the index.jsp.
EDITED
Eclipse SecondServlet.java
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Object[] param = new MWArray[2];
Object[] result = null;
param[0] = new MWNumericArray(Integer.parseInt(request
.getParameter("tf_sim")), MWClassID.DOUBLE);
String[] str_elems = request.getParameter("y0").split("\\s+");
int[] numbers = new int[str_elems.length];
for (int i = 0; i < str_elems.length; i++) {
numbers[i] = Integer.parseInt(str_elems[i]);
}
try {
result = pkModel.model(1, param);
WebFigure webFig = (WebFigure) ((MWJavaObjectRef) result[0]).get();
// Set the figure scope to session
request.getSession().setAttribute("Project_Figure", webFig);
// Bind the figure's lifetime to session
request.getSession().setAttribute("Project_Figure_Binder",
new MWHttpSessionBinder(webFig));
updateSession(request.getSession(), result);
RequestDispatcher dispatcher = request
.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
} catch (MWException e) {
e.printStackTrace();
} finally {
MWArray.disposeArray(result);
}
}
public void updateSession(HttpSession session, Object[] param) {
int outputCount = param.length - 1;
session.setAttribute("numOutputs", outputCount);
}
Really hope someone could help. Thanks!
Take a look at the error message:
java.lang.NumberFormatException: For input string: "1 2 0 0"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
SecondServlet.doGet(SecondServlet.java:70)
The first line states that the problem is the number format, the third tells us that it encounter the problem when using the parseInt method, and the last line tells us it's in the doGet method.
Taking a quick look at the doGet method, we see:
param[1] = new MWNumericArray(Integer.parseInt(request
.getParameter("y0")), MWClassID.DOUBLE);
Now, judging by your comments on the data format earlier y0 is meant to be an array of four numbers. The problem then is that Interger.parseInt is meant for parsing single numbers, not arrays of them.
To solve this you would need to add a separate step to split your input into individual numbers and parse them one at a time. Something like this:
String[] str_elems = request.getParameter("y0").split(" ");
List<Integer> int_elems = new LinkedList<Integer>();
for (int i = 0; i < str_elems.length; i++)
int_elems.add(Integer.parseInt(str_elems[i]));
Or something similar, modified to account for the input preferences of MWNumericArray, documentation for which I couldn't find.

Java/groovy library to solve for a variable algebraically

If I have an equation y=3x I can use "algebra" to make the equation x=y/3. Is there something I can give like:
def y = {x-> x*3}
def x = ThisWouldBeNice.solveForMe(y, 'x') //does the same as: def x = {y-> y/3}
I thought JScience could do this, but I can't seem to figure it out if its there.
I think WolframAlpha API can do that, try something like that in the explorer http://products.wolframalpha.com/api/explorer.html
y = 3x; y=6; x?
Some time ago I did something similar (actually a quick & dirty test) with Wolfram Alpha API as denis.solonenko suggests. To achieve it, use "f = 3*x", as the api response is a bit different and includes the solution for the variable x.
This code -includes a copy of the response- (use a correct API id to get it directly) solves it.
import java.net.URLEncoder
def stringEquation = "f = 3 * x"
def equation = { x -> 3*x }
//def response = "http://api.wolframalpha.com/v2/query?appid=xxx&input=" + URLEncoder.encode(stringEquation) + "&format=plaintext".toURL().text
def response= """<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='true'
error='false'
numpods='6'
datatypes='Geometry'
timedout=''
timing='0.766'
parsetiming='0.181'
parsetimedout='false'
recalculate=''
id='MSP6141a0482cfc786eibg000036bb0i3bhf6d6aih&s=50'
related='http://www4a.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSP6151a0482cfc786eibg00005a4g16ee5ei232bf&s=50'
version='2.1'>
<pod title='Input'
scanner='Identity'
id='Input'
position='100'
error='false'
numsubpods='1'>
<subpod title=''>
<plaintext>f = 3 x</plaintext>
</subpod>
</pod>
<pod title='Geometric figure'
scanner='Geometry'
id='GeometricFigure (ofBoundary)'
position='200'
error='false'
numsubpods='1'>
<subpod title=''>
<plaintext>line</plaintext>
</subpod>
<states count='1'>
<state name='Properties'
input='GeometricFigure (ofBoundary)__Properties' />
</states>
</pod>
<pod title='Plot'
scanner='Plotter'
id='Plot'
position='300'
error='false'
numsubpods='1'>
<subpod title=''>
<plaintext></plaintext>
</subpod>
</pod>
<pod title='Alternate form'
scanner='Simplification'
id='AlternateForm'
position='400'
error='false'
numsubpods='1'>
<subpod title=''>
<plaintext>f-3 x = 0</plaintext>
</subpod>
</pod>
<pod title='Solution for the variable x'
scanner='Reduce'
id='SolutionForTheVariableV'
position='500'
error='false'
numsubpods='1'
primary='true'>
<subpod title=''
primary='true'>
<plaintext>x = f/3</plaintext>
</subpod>
</pod>
<pod title='Implicit derivatives'
scanner='ImplicitDifferentiation'
id='ImplicitDerivatives'
position='600'
error='false'
numsubpods='2'>
<subpod title=''>
<plaintext>(dx(f))/(df) = 1/3</plaintext>
</subpod>
<subpod title=''>
<plaintext>(df(x))/(dx) = 3</plaintext>
</subpod>
<states count='1'>
<state name='More'
input='ImplicitDerivatives__More' />
</states>
</pod>
</queryresult>"""
def queryresult = new XmlSlurper().parseText(response)
def solution = queryresult.pod.findAll { it.#title.text() == "Solution for the variable x" }.toString()
It returns: x = f/3
NOTE: check print the closure definition/source in Groovy if you need to deal with the actual closure code.

Categories

Resources