[kepler-code] r28823 - in trunk/modules: science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship science-pipes-frontend/WebContent/editor/parts

barseghian at ecoinformatics.org barseghian at ecoinformatics.org
Thu Oct 20 16:00:04 PDT 2011


Author: barseghian
Date: 2011-10-20 16:00:04 -0700 (Thu, 20 Oct 2011)
New Revision: 28823

Modified:
   trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/LifeTable.java
   trunk/modules/science-pipes-frontend/WebContent/editor/parts/edu.cornell.birds.is.bap.actor.survivorship.LifeTable.meta.xml
Log:
improvements to LifeTable actor. now calculating dx and qx, rearrange to more closely resemble table in DECA Demography doc, don't include R cruft in output

Modified: trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/LifeTable.java
===================================================================
--- trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/LifeTable.java	2011-10-20 21:17:18 UTC (rev 28822)
+++ trunk/modules/science-pipes-backend/src/edu/cornell/birds/is/bap/actor/survivorship/LifeTable.java	2011-10-20 23:00:04 UTC (rev 28823)
@@ -33,11 +33,18 @@
 package edu.cornell.birds.is.bap.actor.survivorship;
 
 
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.ecoinformatics.seek.R.RExpression;
 
 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;
@@ -52,9 +59,9 @@
 
 	public static Log log = LogFactory.getLog(LifeTable.class);
 
-	//public TypedIOPort outPort;
 	public TypedIOPort dataPort;
-	
+	public TypedIOPort lifeTablePort;
+		
 
 	/** Construct an actor with the given container and name.
 	 *  @param container The container.
@@ -70,33 +77,87 @@
 		
         dataPort = new TypedIOPort(this, "dataPort", true, false);
         //dataPort.setTypeEquals(BaseType.GENERAL);    	
-    	
-		//outPort = new TypedIOPort(this, "outPort", false, true);
-		//outPort.setTypeEquals(BaseType.OBJECT);
-		//outPort.setTypeEquals(BaseType.STRING);
 
+        lifeTablePort = new TypedIOPort(this, "lifeTablePort", false, true);
+        lifeTablePort.setTypeEquals(BaseType.STRING);
+
 		expression.setExpression("filled in when actor fires");
 	}
 
 	public void fire() throws IllegalActionException {
 		
-		//TODO should probably change from summary to a dataframe of our making
+		String outputFile = home + "lifetable.txt";
+		
 		String survivalCurveRcode = "" +
-			"demographyData.survfit <- dataPort\n" + 
+			"demographyData.survFit <- dataPort\n" + 
 			"library(survival)\n" +
-			//"sink(\""+home+"lifetable.txt\")\n" + 
-			"summary(demographyData.survfit)\n";
-			//"outPort <- summary(demographyData.survfit)\n" +
-			//"sink()\n" +
-			//"readLines(\""+home+"lifetable.txt\")\n";
-			//"outPort <- \""+home+"lifetable.txt\"\n" +
-			//"outPort <- capture.output(summary(demographyData.survfit))\n" + 
-			//"outPort\n";
+			"temp <- as.data.frame(summary(demographyData.survFit)[c(\"time\",\"n.risk\",\"n.event\",\"surv\",\"std.err\", \"strata\")])\n" + 
+			"stratums <- unique(temp$strata)\n" + 
+			"numLevels <- length(stratums)\n" + 
+			"\n" + 
+			"lifetables <- list()\n" + 
+			"finallifetables <- list()\n" + 
+			"for (i in 1:numLevels){\n" + 
+			"   level <- subset(temp, strata==stratums[i])\n" +
+			"   lifetables[[i]] <- level[,!(names(level) %in% \"strata\")]\n" +
+			"}" +
+			"\n" +
+			"#calculate dx and qx\n" + 
+			"for (j in 1:numLevels){\n" + 
+			"   for(i in 1:length(lifetables[[j]]$surv)){\n" + 
+			"      if (i==length(lifetables[[j]]$surv)){\n" +
+			"         lifetables[[j]]$dx[i] = NA\n" +
+		    "         lifetables[[j]]$qx[i] = NA\n" + 
+			"      }\n" + 
+			"      else {\n" + 
+			"         lifetables[[j]]$dx[i] = lifetables[[j]]$surv[i] - lifetables[[j]]$surv[i+1]\n" +
+		    "         lifetables[[j]]$qx[i] = lifetables[[j]]$dx[i]/lifetables[[j]]$surv[i]\n" + 
+			"      }\n" + 
+			"   }\n" +
+			"   # rearrange columns, put dx and qx next to lx\n" +
+			"   finallifetables[[j]] <- lifetables[[j]][c(\"time\", \"n.risk\", \"n.event\", \"surv\", \"dx\", \"qx\", \"std.err\")]\n" + 
+			"}\n" +
+			"\n" +
+			"names(finallifetables) <- stratums\n" + 
+			"\n" + 
+			"sink(\""+outputFile+"\")\n" +
+			"finallifetables\n" + 
+			"sink()\n";
 		
 		expression.setExpression(survivalCurveRcode);
 		
         super.fire();
+        
+        File lifeTableFile = new File(outputFile);
+        if (lifeTableFile.canRead()){
+			try {
+				String lifeTable = readFileAsString(lifeTableFile);
+	        	lifeTablePort.broadcast(new StringToken(lifeTable));
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+        }
+        
 	}
 	
 	
+    /**
+     * Read in a file as a string.
+     *
+     * @param f
+     * @return
+     * @throws java.io.IOException
+     */
+    public static String readFileAsString(File file) throws java.io.IOException {
+            int fLength = (int) file.length();
+        byte[] buffer = new byte[fLength];
+        FileInputStream fis = new FileInputStream(file);
+        BufferedInputStream f = new BufferedInputStream(fis);
+        f.read(buffer);
+        f.close();
+        return new String(buffer);
+    }
+
+	
 }

Modified: trunk/modules/science-pipes-frontend/WebContent/editor/parts/edu.cornell.birds.is.bap.actor.survivorship.LifeTable.meta.xml
===================================================================
--- trunk/modules/science-pipes-frontend/WebContent/editor/parts/edu.cornell.birds.is.bap.actor.survivorship.LifeTable.meta.xml	2011-10-20 21:17:18 UTC (rev 28822)
+++ trunk/modules/science-pipes-frontend/WebContent/editor/parts/edu.cornell.birds.is.bap.actor.survivorship.LifeTable.meta.xml	2011-10-20 23:00:04 UTC (rev 28823)
@@ -13,8 +13,8 @@
 		<property name="port:dataPort" class="ptolemy.kernel.util.ConfigurableAttribute">
 			<configure>Data to be summarized in life table.</configure>
 		</property>
-		<property name="port:output" class="ptolemy.kernel.util.ConfigurableAttribute">
-			<configure>Life table data.</configure>
+		<property name="port:lifeTablePort" class="ptolemy.kernel.util.ConfigurableAttribute">
+			<configure>Life table.</configure>
 		</property>
 	</property>
 	<property name="_iconSmall" class="ptolemy.kernel.util.StringAttribute" value="icon_generic.png">
@@ -30,7 +30,7 @@
 		<property name="input"/>
 		<display name="Data Input"/>
 	</port>
-	<port name="output" class="ptolemy.actor.TypedIOPort">
+	<port name="lifeTablePort" class="ptolemy.actor.TypedIOPort">
 		<property name="output"/>
 		<display name="Life Table"/>
 	</port>



More information about the Kepler-cvs mailing list