You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2004/06/01 23:28:06 UTC

cvs commit: jakarta-commons/math/src/experimental/org/apache/commons/math/stat/univariate BeanListUnivariateImplTest.java BeanListUnivariateImpl.java

mdiggory    2004/06/01 14:28:06

  Modified:    math/src/test/org/apache/commons/math/stat/univariate
                        ListUnivariateImplTest.java ListUnivariateImpl.java
  Added:       math/src/experimental/org/apache/commons/math/stat/univariate
                        BeanListUnivariateImplTest.java
                        BeanListUnivariateImpl.java
  Removed:     math/src/test/org/apache/commons/math/stat/univariate
                        BeanListUnivariateImplTest.java
                        BeanListUnivariateImpl.java
  Log:
  Removing BeanListUnivariate example from test cases. Improving ListUnivariate Serialization Example.
  
  Revision  Changes    Path
  1.3       +39 -2     jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/ListUnivariateImplTest.java
  
  Index: ListUnivariateImplTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/ListUnivariateImplTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ListUnivariateImplTest.java	23 May 2004 00:56:15 -0000	1.2
  +++ ListUnivariateImplTest.java	1 Jun 2004 21:28:06 -0000	1.3
  @@ -18,6 +18,8 @@
   import java.util.ArrayList;
   import java.util.List;
   
  +import org.apache.commons.math.TestUtils;
  +
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  @@ -29,6 +31,7 @@
    */
   
   public final class ListUnivariateImplTest extends TestCase {
  +    
       private double one = 1;
       private float two = 2;
       private int three = 3;
  @@ -134,6 +137,40 @@
   
   
       }
  -
  +    
  +    /** test stats */
  +    public void testSerialization() {
  +        
  +        DescriptiveStatistics u = null;
  +        
  +        try {
  +            u = DescriptiveStatistics.newInstance(ListUnivariateImpl.class);
  +        } catch (InstantiationException e) {
  +            fail(e.getMessage());
  +        } catch (IllegalAccessException e) {
  +            fail(e.getMessage());
  +        }
  +        
  +        assertEquals("total count",0,u.getN(),tolerance);
  +        u.addValue(one);
  +        u.addValue(two);
  +        
  +        DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
  + 
  +        u2.addValue(two);
  +        u2.addValue(three);
  +        
  +        assertEquals("N",n,u2.getN(),tolerance);
  +        assertEquals("sum",sum,u2.getSum(),tolerance);
  +        assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
  +        assertEquals("var",var,u2.getVariance(),tolerance);
  +        assertEquals("std",std,u2.getStandardDeviation(),tolerance);
  +        assertEquals("mean",mean,u2.getMean(),tolerance);
  +        assertEquals("min",min,u2.getMin(),tolerance);
  +        assertEquals("max",max,u2.getMax(),tolerance);
  +
  +        u2.clear();
  +        assertEquals("total count",0,u2.getN(),tolerance);    
  +    }       
   }
   
  
  
  
  1.4       +18 -6     jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/ListUnivariateImpl.java
  
  Index: ListUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/ListUnivariateImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ListUnivariateImpl.java	23 May 2004 00:33:41 -0000	1.3
  +++ ListUnivariateImpl.java	1 Jun 2004 21:28:06 -0000	1.4
  @@ -15,19 +15,24 @@
    */
   package org.apache.commons.math.stat.univariate;
   
  +import java.io.Serializable;
  +import java.util.ArrayList;
   import java.util.List;
   
   import org.apache.commons.math.MathException;
   import org.apache.commons.math.stat.univariate.UnivariateStatistic;
  -import org.apache.commons.math.stat.univariate.AbstractDescriptiveStatistics;
  +import org.apache.commons.math.stat.univariate.DescriptiveStatistics;
   import org.apache.commons.math.util.DefaultTransformer;
   import org.apache.commons.math.util.NumberTransformer;
   
   /**
    * @version $Revision$ $Date$
    */
  -public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
  +public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable {
   
  +    /** Serializable version identifier */
  +    static final long serialVersionUID = -8837442489133392138L;
  +    
       /**
        * Holds a reference to a list - GENERICs are going to make
        * out lives easier here as we could only accept List<Number>
  @@ -40,6 +45,13 @@
       /** hold the window size **/
       protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW;
   
  +	/**
  +	 * No argument Constructor
  +	 */
  +	public ListUnivariateImpl(){
  +	    this(new ArrayList());
  +	}
  +	
       /**
        * Construct a ListUnivariate with a specific List.
        * @param list The list that will back this DescriptiveStatistics
  @@ -196,8 +208,8 @@
       	}
       }
       	
  -    	public int getWindowSize() {
  -    		return windowSize;
  -    	}
  +    public int getWindowSize() {
  +    	return windowSize;
  +    }
   
   }
  
  
  
  1.1                  jakarta-commons/math/src/experimental/org/apache/commons/math/stat/univariate/BeanListUnivariateImplTest.java
  
  Index: BeanListUnivariateImplTest.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.math.stat.univariate;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.apache.commons.math.TestUtils;
  import org.apache.commons.math.stat.StatUtils;
  
  /**
   * Test cases for the {@link BeanListUnivariateImpl} class.
   *
   * @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
   */
  
  public final class BeanListUnivariateImplTest extends TestCase {
      
      private double one = 1;
      private float two = 2;
      private int three = 3;
      private double mean = 2;
      private double sumSq = 18;
      private double sum = 8;
      private double var = 0.666666666666666666667;
      private double std = Math.sqrt(var);
      private double n = 4;
      private double min = 1;
      private double max = 3;
      private double skewness = 0;
      private double kurtosis = 0.5;
      private double tolerance = 10E-15;
      
      
      private List patientList = null;
      
      public BeanListUnivariateImplTest(String name) {
          super(name);
      }
      
      public void setUp() {  
          patientList = new ArrayList();
  
          // Create and add patient bean 1
          VitalStats vs1 = new VitalStats( new Double(120.0), 
                                           new Double(96.4) );
          Patient p1 = new Patient( vs1, new Integer( 35 ) );
          patientList.add( p1 );
  
          // Create and add patient bean 2
          VitalStats vs2 = new VitalStats( new Double(70.0), 
                                           new Double(97.4) );
          Patient p2 = new Patient( vs2, new Integer( 23 ) );
          patientList.add( p2 );
  
          // Create and add patient bean 3
          VitalStats vs3 = new VitalStats( new Double(90.0), 
                                           new Double(98.6) );
          Patient p3 = new Patient( vs3, new Integer( 42 ) );
          patientList.add( p3 );
      }
      
      public static Test suite() {
          TestSuite suite = new TestSuite(BeanListUnivariateImplTest.class);
          suite.setName("Frequency Tests");
          return suite;
      }
      
      /** test stats */
      public void testStats() {	
          DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
          double[] values = {35d, 23d, 42d};
          assertEquals("total count",3,u.getN(),tolerance);
          assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
          assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
          assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
          assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);       
          u.clear();
          assertEquals("total count",0,u.getN(),tolerance);    
      }   
      
      public void testPropStats() {
  
          DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList,
                                            "vitalStats.heartRate" );       
  
          assertEquals( "Mean heart rate unexpected", 93.333, 
                        heartU.getMean(), 0.001 );
          assertEquals( "Max heart rate unexpected", 120.0, 
                        heartU.getMax(), 0.001 );
  
          DescriptiveStatistics ageU = new BeanListUnivariateImpl( patientList,
                                                             "age" );
  
          assertEquals( "Mean age unexpected", 33.333,
                        ageU.getMean(), 0.001 );
          assertEquals( "Max age unexpected", 42.0,
                        ageU.getMax(), 0.001 );
  
      }
      
      public void testSetPropertyName(){
          BeanListUnivariateImpl u = new BeanListUnivariateImpl(null);
          String expected = "property";
          u.setPropertyName(expected);
          assertEquals(expected, u.getPropertyName());
      }
      
      public void testAddValue() {
          DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
          u.addValue(10);
          double[] values = {35d, 23d, 42d, 10d};
          assertEquals("total count",4,u.getN(),tolerance);
          assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
          assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
          assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
          assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);       
          u.clear();
          assertEquals("total count",0,u.getN(),tolerance);      
      }
      
      /** test stats */
      public void testSerialization() {
          
          double[] values = {35d, 23d, 42d};
          
          DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
          assertEquals("total count",3,u.getN(),tolerance);
          assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
          assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
          assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
          assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);   
          
          
          DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
          assertEquals("total count",3,u2.getN(),tolerance);
          assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
          assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
          assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
          assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);   
  
          u.clear();
          assertEquals("total count",0,u.getN(),tolerance);    
          
          u2.clear();
          assertEquals("total count",0,u2.getN(),tolerance);
              
      }    
      
      public class VitalStats {
  
          private Double heartrate;
          private Double temperature;
  
          public VitalStats() {
          }
  
          public VitalStats(Double heartrate, Double temperature) {
              setHeartRate( heartrate );
              setTemperature( temperature );
          }
  
          public Double getHeartRate() {
              return heartrate;
          }
  
          public void setHeartRate(Double heartrate) {
              this.heartrate = heartrate;
          }
  
          public Double getTemperature() {
              return temperature;
          }
  
          public void setTemperature(Double temperature) {
              this.temperature = temperature;
          }
      }
      
      public class Patient {
  
          private VitalStats vitalStats;
          private Integer age;
  
          public Patient() {
          }
  
          public Patient(VitalStats vitalStats, Integer age) {
              setVitalStats( vitalStats );
              setAge( age );
          }
  
          public VitalStats getVitalStats() {
              return( vitalStats );
          }
  
          public void setVitalStats(VitalStats vitalStats) {
              this.vitalStats = vitalStats;
          }
  
          public Integer getAge() {
              return age;
          }
  
          public void setAge(Integer age) {
              this.age = age;
          }
      }
  }
  
  
  
  
  1.1                  jakarta-commons/math/src/experimental/org/apache/commons/math/stat/univariate/BeanListUnivariateImpl.java
  
  Index: BeanListUnivariateImpl.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.math.stat.univariate;
  
  import java.io.Serializable;
  import java.lang.reflect.InvocationTargetException;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.commons.beanutils.PropertyUtils;
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.commons.beanutils.BasicDynaClass;
  import org.apache.commons.beanutils.DynaProperty;
  import org.apache.commons.math.MathException;
  import org.apache.commons.math.util.NumberTransformer;
  
  /**
   * This implementation of DescriptiveStatistics uses commons-beanutils to gather
   * univariate statistics for a List of Java Beans by property.  This 
   * implementation uses beanutils' PropertyUtils to get a simple, nested,
   * indexed, mapped, or combined property from an element of a List.
   * @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
   */
  public class BeanListUnivariateImpl extends ListUnivariateImpl implements Serializable {
  
      /** Serializable version identifier */
      static final long serialVersionUID = -6428201899045406285L;
      
  	/**
  	 * propertyName of the property to get from the bean
  	 */
  	private String propertyName;
  
  	/**
  	 * No argument Constructor
  	 */
  	public BeanListUnivariateImpl(){
  	    this(new ArrayList());
  	}
  	
  	/**
  	 * Construct a BeanListUnivariate with specified
  	 * backing list
  	 * @param list Backing List
  	 */
  	public BeanListUnivariateImpl(List list) {
  		this(list, null);
  	}
  
  	/**
  	 * Construct a BeanListUnivariate with specified
  	 * backing list and propertyName
  	 * @param list Backing List
  	 * @param propertyName Bean propertyName
  	 */
  	public BeanListUnivariateImpl(List list, String propertyName) {
  		super(list);
  		setPropertyName(propertyName);
  	}
  
  	/**
  	 * @return propertyName
  	 */
  	public String getPropertyName() {
  		return propertyName;
  	}
  
  	/**
  	 * @param propertyName Name of Property
  	 */
  	public void setPropertyName(String propertyName) {
  		this.propertyName = propertyName;
  		this.transformer = new NumberTransformer() {
  
  			/**
  			 * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
  			 */
  			public double transform(final Object o) throws MathException {
  				try {
  					return (
  						(Number) PropertyUtils.getProperty(
  							o,
  							getPropertyName()))
  						.doubleValue();
  				} catch (IllegalAccessException e) {
  					throw new MathException(
  						"IllegalAccessException in Transformation: "
  							+ e.getMessage(),
  						e);
  				} catch (InvocationTargetException e) {
  					throw new MathException(
  						"InvocationTargetException in Transformation: "
  							+ e.getMessage(),
  						e);
  				} catch (NoSuchMethodException e) {
  					throw new MathException(
  						"oSuchMethodException in Transformation: "
  							+ e.getMessage(),
  						e);
  				}
  			}
  		};
  	}
  
  	/**
  	  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  	  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  	  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  	  *  and adds the DynaBean to the underlying list.
  	  *
  	  */
  	public void addValue(double v)  {
  	    DynaProperty[] props = new DynaProperty[] {
  	            new DynaProperty(propertyName, Double.class)
  	    };
  	    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
  	    DynaBean dynaBean = null;
  	    try {
  	        dynaBean = dynaClass.newInstance();
  	    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
  	        throw new RuntimeException(ex);   // should never happen
  	    }
  		dynaBean.set(propertyName, new Double(v));
  		addObject(dynaBean);
  	}
  
  	/**
  	 * Adds a bean to this list. 
  	 *
  	 * @param bean Bean to add to the list
  	 */
  	public void addObject(Object bean) {
  		list.add(bean);
  	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org