[kepler-code] r28793 - trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship

barseghian at ecoinformatics.org barseghian at ecoinformatics.org
Thu Oct 13 20:19:24 PDT 2011


Author: barseghian
Date: 2011-10-13 20:19:24 -0700 (Thu, 13 Oct 2011)
New Revision: 28793

Added:
   trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurveCalc.java
   trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurvePlot.java
Log:
separate actors for calculating survival curve(s), and display

Added: trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurveCalc.java
===================================================================
--- trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurveCalc.java	                        (rev 0)
+++ trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurveCalc.java	2011-10-14 03:19:24 UTC (rev 28793)
@@ -0,0 +1,135 @@
+/**
+ *    '$RCSfile: RExpression.java,v $'
+ *
+ *     '$Author: pea1 $'
+ *       '$Date: 2009/03/25 15:51:49 $'
+ *   '$Revision: 1.2 $'
+ *
+ *  For Details: http://kepler.ecoinformatics.org
+ *
+ * Copyright (c) 2003 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
+ * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
+ * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
+ * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
+ * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+package edu.cornell.birds.is.bap.actor.survivorship;
+
+import java.io.File;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.ecoinformatics.seek.R.RExpression;
+
+import edu.cornell.birds.is.bap.ptolemy.StringChoiceParameter;
+import edu.cornell.birds.is.bap.ptolemy.StringChoiceProvider;
+
+import ptolemy.actor.TypedIOPort;
+import ptolemy.data.StringToken;
+import ptolemy.data.type.BaseType;
+import ptolemy.kernel.CompositeEntity;
+import ptolemy.kernel.util.IllegalActionException;
+import ptolemy.kernel.util.NameDuplicationException;
+
+
+public class SurvivalCurveCalc extends RExpression implements StringChoiceProvider {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 3628449403386107672L;
+
+	public static Log log = LogFactory.getLog(SurvivalCurveCalc.class);
+
+	public TypedIOPort outPort;
+	public TypedIOPort dataPort;
+	
+    public StringChoiceParameter groupByText = null;
+	
+
+	/** Construct an actor with the given container and name.
+	 *  @param container The container.
+	 *  @param name The name of this actor.
+	 *  @exception IllegalActionException If the actor cannot be contained
+	 *   by the proposed container.
+	 *  @exception NameDuplicationException If the container already has an
+	 *   actor with this name.
+	 */
+	public SurvivalCurveCalc(CompositeEntity container, String name)
+	throws NameDuplicationException, IllegalActionException  {
+		super(container, name);
+		
+        dataPort = new TypedIOPort(this, "dataPort", true, false);
+        //dataPort.setTypeEquals(BaseType.GENERAL);    	
+    	
+		outPort = new TypedIOPort(this, "out", false, true);
+		outPort.setTypeEquals(BaseType.OBJECT);
+		
+		groupByText = new StringChoiceParameter(this, "group", this);
+		groupByText.setDisplayName("Group By Column");
+		groupByText.setExpression(groupByText.getChoices()[0]);
+
+		expression.setExpression("filled in when actor fires");
+	}
+
+	public void fire() throws IllegalActionException {
+		
+		String groupByChoice = groupByText.stringValue();
+		String key = groupByText.getKeyForCurrentChoice();
+		log.debug("groupByText: " + key);
+		
+    	StringToken dataToken = (StringToken) dataPort.get(0);
+    	String demographyFileString = dataToken.stringValue();
+    	File demographyFile = new File(demographyFileString);
+    	if (!demographyFile.canRead()){
+    		System.out.println("SurvivalCurve.fire() failed to get readable file from dataPort input port");
+    		return;
+    	}
+
+		// TODO: could probably change from read.table to read.csv
+		// XXX currently allowing strings with single quotes inside that are not in double quotes
+		String survivalCurveRcode = "" +
+			"demographyData <- read.table(\""+demographyFile.toString()+"\", sep=\",\", quote=\"\\\"\", header = TRUE)\n"+
+			"library(survival)\n" +
+			"attach(demographyData)\n" +
+			"demographyData.survFit <- survfit(Surv(Age) ~ "+groupByChoice+", conf.type=\"none\")\n" + 
+			"out <- demographyData.survFit\n";
+		
+		expression.setExpression(survivalCurveRcode);
+		
+        super.fire();
+	}
+	
+	// XXX would be nice to utilize names or colnames of table for providing groupBy choices,
+	// but only get data on fire. possible to change when port gets connected?
+	private String[] choices = new String[] {
+		"Gender"
+	};
+	public String[] getChoices() {
+		return choices;
+	}
+	public String getValueForChoice(String choice) {
+		return choice;
+	}
+	public String getChoiceForValue(String value) {
+		return value;
+	}
+	
+}


Property changes on: trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurveCalc.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurvePlot.java
===================================================================
--- trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurvePlot.java	                        (rev 0)
+++ trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurvePlot.java	2011-10-14 03:19:24 UTC (rev 28793)
@@ -0,0 +1,230 @@
+/**
+ *    '$RCSfile: RExpression.java,v $'
+ *
+ *     '$Author: pea1 $'
+ *       '$Date: 2009/03/25 15:51:49 $'
+ *   '$Revision: 1.2 $'
+ *
+ *  For Details: http://kepler.ecoinformatics.org
+ *
+ * Copyright (c) 2003 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
+ * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
+ * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
+ * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
+ * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+package edu.cornell.birds.is.bap.actor.survivorship;
+
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.ecoinformatics.seek.R.RExpression;
+import org.jfree.chart.ChartUtilities;
+
+import edu.cornell.birds.is.bap.actor.WorkflowOutputRepositoryActorHelper;
+
+import ptolemy.actor.TypedIOPort;
+import ptolemy.actor.parameters.PortParameter;
+import ptolemy.data.AWTImageToken;
+import ptolemy.data.StringToken;
+import ptolemy.data.Token;
+import ptolemy.data.expr.Parameter;
+import ptolemy.data.type.BaseType;
+import ptolemy.kernel.CompositeEntity;
+import ptolemy.kernel.util.IllegalActionException;
+import ptolemy.kernel.util.NameDuplicationException;
+
+
+
+public class SurvivalCurvePlot extends RExpression {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 3628449403386107672L;
+
+	public static Log log = LogFactory.getLog(SurvivalCurvePlot.class);
+
+	public PortParameter xLabelPort;
+	public PortParameter yLabelPort;
+	public TypedIOPort outPort;
+	public PortParameter titlePort;
+	public PortParameter auxTitlePort;
+	public Parameter widthParameter;
+	public Parameter heightParameter;
+	public TypedIOPort dataPort;
+	
+	protected WorkflowOutputRepositoryActorHelper outputServiceHelper = new WorkflowOutputRepositoryActorHelper();
+	
+
+	/** Construct an actor with the given container and name.
+	 *  @param container The container.
+	 *  @param name The name of this actor.
+	 *  @exception IllegalActionException If the actor cannot be contained
+	 *   by the proposed container.
+	 *  @exception NameDuplicationException If the container already has an
+	 *   actor with this name.
+	 */
+	public SurvivalCurvePlot(CompositeEntity container, String name)
+	throws NameDuplicationException, IllegalActionException  {
+		super(container, name);
+		
+		xLabelPort = new PortParameter(this, "xLabel");
+		xLabelPort.setDisplayName("X-Axis Label");
+		xLabelPort.setStringMode(true);
+		xLabelPort.setExpression("Age");
+		
+		yLabelPort = new PortParameter(this, "yLabel");
+		yLabelPort.setDisplayName("Y-Axis Label");
+		yLabelPort.setStringMode(true);
+		yLabelPort.setExpression("Survival Probability");
+		
+        dataPort = new TypedIOPort(this, "dataPort", true, false);
+		
+		titlePort = new PortParameter(this, "title");
+		titlePort.setDisplayName("Title");
+		titlePort.setStringMode(true);
+		
+		auxTitlePort = new PortParameter(this, "auxiliaryTitle");
+		auxTitlePort.setDisplayName("Auxiliary Title");
+		auxTitlePort.setStringMode(true);
+
+		widthParameter = new Parameter(this, "width");
+		widthParameter.setTypeEquals(BaseType.INT);
+		widthParameter.setExpression("700");
+		
+		heightParameter = new Parameter(this, "height");
+		heightParameter.setTypeEquals(BaseType.INT);
+		heightParameter.setExpression("500");
+
+		
+		outPort = new TypedIOPort(this, "out", false, true);
+		outPort.setTypeEquals(BaseType.OBJECT);
+		
+		
+		expression.setExpression("this is filled in at fire()");
+	}
+
+	public void fire() throws IllegalActionException {
+
+		// XXX currently we want png not pdf. see about support for pdf later.
+		forcePDFForOSX = false;
+		
+		String title = getCompoundTitle();
+        String xlabel = getXLabel();
+        String ylabel = getYLabel();
+        		
+        //XXX a bit hacky:
+        numXPixels.setExpression(widthParameter.getExpression());
+		numYPixels.setExpression(heightParameter.getExpression());
+		
+		String survivalCurveRcode = "" +
+			"demographyData.survfit <- dataPort\n" + 
+			"library(survival)\n" +
+			"plot(demographyData.survfit, xlab=\""+xlabel+"\", ylab=\""+ylabel+"\", conf.int = TRUE, col = c(\"red\",\"blue\"), lty = 1:1)\n" +
+			"legend(\"topright\", inset=.05, c(\"Female\",\"Male\"), col = c(\"red\", \"blue\"), lty=1:1, horiz=FALSE)\n" +
+			"title(main=\""+title+"\")\n";
+		
+		expression.setExpression(survivalCurveRcode);
+		
+        // *****
+		super.fire();
+        // *****
+		        
+        BufferedImage bufferedImage = null;
+        try {
+        	bufferedImage = ImageIO.read(new File(home+graphicsOutputFile));
+        	if (bufferedImage == null){
+				System.out.println("SurvivalCurvePlot.fire bufferedImage == null ERROR");
+        	}
+        	storeOutput(bufferedImage);
+        	outPort.send(0, new AWTImageToken(bufferedImage));
+        } catch (IOException e) {
+        	// TODO Auto-generated catch block
+        	e.printStackTrace();
+        }
+        
+	}
+	
+	
+	protected void storeOutput(BufferedImage image) {
+		if (outputServiceHelper.isActive()) {
+			try {
+				byte[] bytes = ChartUtilities.encodeAsPNG(image);
+				outputServiceHelper.storeOutput(this, "image/png", null, bytes);
+			}
+			catch (IOException e) {
+				log.warn("IOException trying to encode image for storage", e);
+			}
+		}
+	}
+	
+    protected String getCompoundTitle() throws IllegalActionException {
+
+    	StringBuffer b = new StringBuffer();
+    	
+    	titlePort.update();
+    	Token titleToken = titlePort.getToken();
+    	
+    	auxTitlePort.update();
+    	Token auxTitleToken = auxTitlePort.getToken(); 
+    	
+    	if (titleToken != null) { 
+    		b.append(((StringToken)titleToken).stringValue());
+    	}
+    	
+    	if (auxTitleToken != null) {
+    		if (b.length() > 0) b.append("\n");
+    		b.append(((StringToken)auxTitleToken).stringValue());
+    	}
+    	
+    	return b.toString();
+    }
+	
+    protected String getXLabel() throws IllegalActionException {
+    	
+    	xLabelPort.update();
+    	Token token = xLabelPort.getToken();
+    	
+    	if (token != null) { 
+    		return ((StringToken)token).stringValue();
+    	}
+    	return null;
+    }
+    
+    protected String getYLabel() throws IllegalActionException {
+    	
+    	yLabelPort.update();
+    	Token token = yLabelPort.getToken();
+    	
+    	if (token != null) { 
+    		return ((StringToken)token).stringValue();
+    	}
+    	return null;
+    }
+	
+	
+}


Property changes on: trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/SurvivalCurvePlot.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain



More information about the Kepler-cvs mailing list