use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedSemPm method setVariablesTemplate.
public void setVariablesTemplate(String variablesTemplate) throws ParseException {
if (variablesTemplate == null) {
throw new NullPointerException();
}
// Test to make sure it's parsable.
ExpressionParser parser = new ExpressionParser();
parser.parseExpression(variablesTemplate);
this.variablesTemplate = variablesTemplate;
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedSemPm method setParametersEstimationInitializationTemplate.
public void setParametersEstimationInitializationTemplate(String parametersTemplate) throws ParseException {
if (parametersTemplate == null) {
throw new NullPointerException();
}
// Test to make sure it's parsable.
ExpressionParser parser = new ExpressionParser();
parser.parseExpression(parametersTemplate);
this.parametersEstimationInitializationTemplate = parametersTemplate;
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedTemplateEditor method listen.
private void listen() {
String expressionString = expressionTextPane.getText();
ExpressionParser parser = new ExpressionParser();
try {
if (!"".equals(expressionString.trim())) {
parser.parseExpression(expressionString);
}
color = Color.BLACK;
start = 0;
stringWidth = expressionString.length();
recolorTime = System.currentTimeMillis();
String startsWithText = startsWithField.getValue().trim();
if ("Variables".equals(combo.getSelectedItem())) {
if (!"".equals(expressionString.trim())) {
// getSemPm().setVariablesTemplate(expressionString);
}
} else if ("Errors".equals(combo.getSelectedItem())) {
if (!"".equals(expressionString.trim())) {
// getSemPm().setErrorsTemplate(expressionString);
}
} else if ("Parameter Initializations".equals(combo.getSelectedItem())) {
if (!"".equals(startsWithText.trim())) {
getSemPm().setStartsWithParametersTemplate(startsWithText, expressionString);
}
// getSemPm().setParametersTemplate(expressionString);
} else if ("Estimation Starting Values".equals(combo.getSelectedItem())) {
if (!"".equals(startsWithText.trim())) {
getSemPm().setStartsWithParametersEstimationInitializaationTemplate(startsWithText, expressionString);
}
// getSemPm().setParametersEstimationInitializationTemplate(expressionString);
}
} catch (ParseException e) {
color = Color.RED;
start = e.getErrorOffset();
stringWidth = parser.getNextOffset() - e.getErrorOffset();
recolorTime = System.currentTimeMillis();
}
this.latestParser = parser;
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedExpressionEditor method listen.
private void listen() {
String expressionString = expressionTextPane.getText();
String valueExpressionString;
ExpressionParser parser = new ExpressionParser(otherVariables, ExpressionParser.RestrictionType.MAY_NOT_CONTAIN);
try {
if (!"".equals(expressionString)) {
parser.parseExpression(expressionString);
}
color = Color.BLACK;
start = 0;
stringWidth = expressionString.length();
recolorTime = System.currentTimeMillis();
valueExpressionString = expressionString;
} catch (ParseException e) {
color = Color.RED;
start = e.getErrorOffset();
stringWidth = parser.getNextOffset() - e.getErrorOffset();
recolorTime = System.currentTimeMillis();
valueExpressionString = null;
}
if (valueExpressionString != null) {
String formula = expressionTextPane.getText();
if (node != null) {
if (node.getNodeType() != NodeType.ERROR && !formula.contains(errorNode.getName()) && errorTermCheckBox.isSelected()) {
if (!formula.trim().endsWith("+") && !"".equals(formula)) {
formula += " + ";
}
formula += errorNode.getName();
}
this.expressionString = formula;
if (node.getNodeType() == NodeType.ERROR) {
resultTextPane.setText(node + " ~ " + formula);
} else {
resultTextPane.setText(node + " = " + formula);
}
} else if (parameter != null) {
this.expressionString = formula;
resultTextPane.setText(parameter + " ~ " + formula);
}
referencedParametersLabel.setText("Parameters: " + parameterString(parser));
}
this.latestParser = parser;
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class Transformation method transformEquation.
// ======================== Private Methods ============================//
/**
* Transforms the given dataset using the given equation.
*/
private static void transformEquation(DataSet data, String eq) throws ParseException {
ExpressionParser parser = new ExpressionParser(data.getVariableNames(), ExpressionParser.RestrictionType.MAY_ONLY_CONTAIN);
Equation equation = parser.parseEquation(eq);
addVariableIfRequired(data, equation.getVariable());
Expression expression = equation.getExpression();
Node variable = data.getVariable(equation.getVariable());
if (variable == null) {
throw new IllegalStateException("Unknown variable " + equation.getVariable());
}
int column = data.getColumn(variable);
// build the context pairs.
List<String> contextVars = getContextVariables(expression);
// now do the transformation row by row.
DataBackedContext context = new DataBackedContext(data, contextVars);
int rows = data.getNumRows();
for (int row = 0; row < rows; row++) {
// build the context
context.setRow(row);
double newValue = expression.evaluate(context);
data.setDouble(row, column, newValue);
}
}
Aggregations