608 | | def dataForGroup = computeMeanAndError( values ); |
| 608 | def dataForGroup = null; |
| 609 | switch( params.get( 'aggregation') ) { |
| 610 | case "average": |
| 611 | dataForGroup = computeMeanAndError( values ); |
| 612 | break; |
| 613 | case "count": |
| 614 | dataForGroup = computeCount( values ); |
| 615 | break; |
| 616 | case "median": |
| 617 | dataForGroup = computeMedian( values ); |
| 618 | break; |
| 619 | case "none": |
| 620 | // Currently disabled, create another function |
| 621 | dataForGroup = computeMeanAndError( values ); |
| 622 | break; |
| 623 | case "sum": |
| 624 | dataForGroup = computeSum( values ); |
| 625 | break; |
| 626 | default: |
| 627 | // Default is "average" |
| 628 | dataForGroup = computeMeanAndError( values ); |
| 629 | } |
| 630 | |
| 953 | |
| 954 | /** |
| 955 | * Computes the median of the given values. Values that can not be parsed to a number |
| 956 | * are ignored. If no values are given, null is returned. |
| 957 | * @param values List of values to compute the median for |
| 958 | * @return Median of the values |
| 959 | */ |
| 960 | protected def computeMedian( List values ) { |
| 961 | def listOfValues = []; |
| 962 | values.each { value -> |
| 963 | def num = getNumericValue( value ); |
| 964 | if( num != null ) { |
| 965 | listOfValues << num; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | listOfValues.sort(); |
| 970 | |
| 971 | def listSize = listOfValues.size(); |
| 972 | |
| 973 | if( listSize > 0 ) { |
| 974 | def listHalf = (int) Math.abs(listSize/2); |
| 975 | if(listSize%2==0) { |
| 976 | // If the list is of an even size, take the mean of the middle two value's |
| 977 | return ["value": (listOfValues.get(listHalf)+listOfValues.get(listHalf-1))/2]; |
| 978 | } else { |
| 979 | // If the list is of an odd size, take the middle value |
| 980 | return ["value": listOfValues.get(listHalf-1)]; |
| 981 | } |
| 982 | } else |
| 983 | return ["value": null]; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Computes the count of the given values. Values that can not be parsed to a number |
| 988 | * are ignored. If no values are given, null is returned. |
| 989 | * @param values List of values to compute the count for |
| 990 | * @return Count of the values |
| 991 | */ |
| 992 | protected def computeCount( List values ) { |
| 993 | def sumOfValues = 0; |
| 994 | def sizeOfValues = 0; |
| 995 | values.each { value -> |
| 996 | def num = getNumericValue( value ); |
| 997 | if( num != null ) { |
| 998 | sumOfValues += num; |
| 999 | sizeOfValues++ |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | if( sizeOfValues > 0 ) |
| 1004 | return ["value": sizeOfValues]; |
| 1005 | else |
| 1006 | return ["value": null]; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Computes the sum of the given values. Values that can not be parsed to a number |
| 1011 | * are ignored. If no values are given, null is returned. |
| 1012 | * @param values List of values to compute the sum for |
| 1013 | * @return Arithmetic sum of the values |
| 1014 | */ |
| 1015 | protected def computeSum( List values ) { |
| 1016 | def sumOfValues = 0; |
| 1017 | def sizeOfValues = 0; |
| 1018 | values.each { value -> |
| 1019 | def num = getNumericValue( value ); |
| 1020 | if( num != null ) { |
| 1021 | sumOfValues += num; |
| 1022 | sizeOfValues++ |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if( sizeOfValues > 0 ) |
| 1027 | return ["value": sumOfValues]; |
| 1028 | else |
| 1029 | return ["value": null]; |
| 1030 | } |