public void TestNoDataDoesNotCauseException()
 {
     IndicatorMetadata metadata = new IndicatorMetadata();
     GroupRoot root = new GroupRoot();
     GetComparisonManager().CompareToCalculateSignficance(root, metadata);
     Assert.AreEqual(0, root.Data.Count);
     Assert.AreEqual(0, root.Grouping.Count);
 }
 public void TestNoExceptionForRegionalComparisonIfNoRegionalData()
 {
     GroupRoot root = new GroupRoot();
     AddRegionalData(root);
     AddNationalData(root);
     root.Grouping[0].ComparatorData = null;
     GetComparisonManager().CompareToCalculateSignficance(root, GetMetadata());
 }
        private void FormatDataAndStats(GroupRoot groupRoot, IndicatorMetadata metadata,
            TimePeriodFormatter timePeriodFormatter)
        {
            var formatter = NumericFormatterFactory.New(metadata, groupDataReader);

            new GroupRootFormatter().
                Format(groupRoot, metadata, timePeriodFormatter, formatter);
        }
 public GroupRootUniqueKey(GroupRoot groupRoot)
 {
     var sb = new StringBuilder();
     sb.Append(groupRoot.IndicatorId);
     sb.Append("-");
     sb.Append(groupRoot.SexId);
     sb.Append("-");
     sb.Append(groupRoot.AgeId);
     Key = sb.ToString();
 }
        public void TestRegionalIsComparedToNational()
        {
            GroupRoot root = new GroupRoot();

            AddRegionalData(root);
            AddNationalData(root);

            GetComparisonManager().CompareToCalculateSignficance(root, GetMetadata());
            Assert.AreEqual((int)Significance.Better,
                root.Grouping[0].ComparatorData.Significance[ComparatorIds.England]);
        }
        public void TestTimePeriodsAreFormatted()
        {
            GroupRoot groupRoot = new GroupRoot();
            groupRoot.Grouping.Add(new Grouping { DataPointYear = 2005, YearRange = 1, DataPointQuarter = -1 });
            IndicatorMetadata metadata = new IndicatorMetadata { YearTypeId = 1 };

            Limits limits = new Limits { Min = 0, Max = 50 };
            var formatter = NumericFormatterFactory.NewWithLimits(metadata, limits);
            new GroupRootFormatter().Format(groupRoot, metadata, new DataPointTimePeriodFormatter(), formatter);

            Assert.AreEqual("2005", groupRoot.Grouping[0].TimePeriodText);
        }
        public void TestMinusOneComparatorsAreIgnored()
        {
            GroupRoot root = new GroupRoot();

            AddRegionalData(root);

            root.Grouping[0].ComparatorId = -1;
            root.Data.Add(new CoreDataSet { Value = 10, LowerCI = 9, UpperCI = 11 });

            GetComparisonManager().CompareToCalculateSignficance(root, GetMetadata());
            Assert.AreEqual(0, root.Data[0].Significance.Count);
        }
        private void CompareAllDataAgainstTarget(GroupRoot groupRoot)
        {
            if (TargetComparer != null)
            {
                // Local area data
                groupRoot.Data.ToList().ForEach(x =>
                    TargetComparer.AddTargetSignificance(x, TargetComparer));

                // Benchmark data
                groupRoot.Grouping.ToList().ForEach(x =>
                     TargetComparer.AddTargetSignificance(x.ComparatorData, TargetComparer));
            }
        }
        public void Format(GroupRoot groupRoot, IndicatorMetadata metadata, TimePeriodFormatter timePeriodFormatter, NumericFormatter formatter)
        {
            foreach (CoreDataSet coreData in groupRoot.Data)
            {
                formatter.Format(coreData);
                formatter.FormatConfidenceIntervals(coreData);
            }

            foreach (var grouping in groupRoot.Grouping)
            {
                formatter.Format(grouping.ComparatorData);
                formatter.FormatConfidenceIntervals(grouping.ComparatorData);
                timePeriodFormatter.Format(grouping, metadata);
            }
        }
 public TrendRoot(GroupRoot rootToCopy)
 {
     IndicatorId = rootToCopy.IndicatorId;
     StateSex = rootToCopy.StateSex;
     StateAge = rootToCopy.StateAge;
     AreaTypeId = rootToCopy.AreaTypeId;
     SexId = rootToCopy.SexId;
     AgeId = rootToCopy.AgeId;
     PolarityId = rootToCopy.PolarityId;
     YearRange = rootToCopy.YearRange;
     ComparatorMethodId = rootToCopy.ComparatorMethodId;
     Age = rootToCopy.Age;
     Sex = rootToCopy.Sex;
     DataPoints = new Dictionary<string, IList<TrendDataPoint>>();
     ComparatorValueFs = new List<Dictionary<int, string>>();
     ComparatorValue = new List<Dictionary<int, double>>();
     Periods = new List<string>();
 }
        private TargetComparer GetTargetComparer(IndicatorMetadata metadata, GroupRoot groupRoot, IArea england)
        {
            var targetComparer = TargetComparerFactory.New(metadata.TargetConfig);

            // Initialise the target comparer
            if (targetComparer as BespokeTargetPercentileRangeComparer != null)
            {
                new TargetComparerHelper(groupDataReader, england)
                    .GetPercentileData(targetComparer, groupRoot.FirstGrouping, metadata);              
            }
            else
            {
                new TargetComparerHelper(groupDataReader, england)
                    .AssignExtraDataIfRequired(england, targetComparer, groupRoot.FirstGrouping, metadata);
            }

            return targetComparer;
        }
        private void CompareRegionalAgainstNational(GroupRoot groupRoot, IndicatorMetadata metadata)
        {
            if (comparer != null &&
                groupRoot.Grouping.Count > 1 &&
                CanCompareSubnationalAgainstNational(groupRoot.FirstGrouping.ComparatorMethodId))
            {
                Grouping regionalGrouping = groupRoot.GetSubnationalGrouping();
                Grouping nationalGrouping = groupRoot.GetNationalGrouping();

                if (nationalGrouping != null && regionalGrouping != null && regionalGrouping.ComparatorData != null)
                {
                    Significance significance = comparer.Compare(regionalGrouping.ComparatorData,
                        nationalGrouping.ComparatorData, metadata);
                    regionalGrouping.ComparatorData.AddSignificance(nationalGrouping.ComparatorId,
                        significance);
                }
            }
        }
        public void TestGetGroupings()
        {
            // Subnational
            GroupRoot root = new GroupRoot()
            {
                Grouping = new List<Grouping> {
                new Grouping{ComparatorId = ComparatorIds.Subnational}
            }};
            Assert.IsNotNull(root.FirstGrouping);

            // National
            root = new GroupRoot()
            {
                Grouping = new List<Grouping> {
                new Grouping{ComparatorId = ComparatorIds.England}
            }
            };
            Assert.IsNotNull(root.FirstGrouping);
        }
        public void TestCopyConstructor()
        {
            GroupRoot root = new GroupRoot {
                AreaTypeId = 2,
                StateSex = true,
                IndicatorId = 3,
                SexId = 5,
                PolarityId = 6,
                AgeId = 7
            };

            TrendRoot rootTrend = new TrendRoot(root);

            Assert.AreEqual(root.IndicatorId, rootTrend.IndicatorId);
            Assert.AreEqual(root.StateSex, rootTrend.StateSex);
            Assert.AreEqual(root.AreaTypeId, rootTrend.AreaTypeId);
            Assert.AreEqual(5, rootTrend.SexId);
            Assert.AreEqual(6, rootTrend.PolarityId);
            Assert.AreEqual(7, rootTrend.AgeId);
        }
        private void CreateNewRoot(List<Grouping> groupingsWithSameIndicatorIdSexIdAndAgeId,
            bool stateSex, bool stateAge)
        {
            // Get distinct comparator groupings (1 & 4) for these indicators
            var comparatorIds = groupingsWithSameIndicatorIdSexIdAndAgeId.Select(x => x.ComparatorId).Distinct();

            var templateGrouping = groupingsWithSameIndicatorIdSexIdAndAgeId.First();
            var newGroupings = new List<Grouping>();
            foreach (var comparatorId in comparatorIds)
            {
                var grouping = new Grouping(templateGrouping) {ComparatorId = comparatorId};
                newGroupings.Add(grouping);
            }

            root = new GroupRoot();
            GroupRoots.Add(root);
            AssignGroupings(newGroupings);
            AssignPropertiesFromGrouping(newGroupings.First());
            root.StateSex = stateSex;
            root.StateAge = stateAge;
        }
        private void CompareLocalAreaData(GroupRoot groupRoot, IndicatorMetadata metadata)
        {
            var groupings = groupRoot.Grouping;
            foreach (var grouping in groupings)
            {
                if (grouping.IsComparatorDefined())
                {
                    comparer = new IndicatorComparerFactory { PholioReader = PholioReader }.New(grouping);
                    ICategoryComparer categoryComparer = comparer as ICategoryComparer;
                    IndicatorComparisonHelper.AssignCategoryDataIfRequired(comparer, grouping, groupDataReader,
                        groupRoot.Data);

                    foreach (CoreDataSet coreData in groupRoot.Data)
                    {
                        coreData.Significance.Add(grouping.ComparatorId, categoryComparer == null
                            ? (int)comparer.Compare(coreData, grouping.ComparatorData, metadata)
                            : categoryComparer.GetCategory(coreData));
                    }
                }
            }
        }
 private Significance GetSignificanceOnly(GroupRoot groupRoot)
 {
     Grouping grouping = groupRoot.GetNationalGrouping();
     Significance significance;
     GetCoreDataSet(grouping, out significance);
     return significance;
 }
 public void CompareToCalculateSignficance(GroupRoot groupRoot, IndicatorMetadata metadata)
 {
     CompareLocalAreaData(groupRoot, metadata);
     CompareAllDataAgainstTarget(groupRoot);
     CompareRegionalAgainstNational(groupRoot, metadata);
 }
        private void AssignComparatorData(GroupRoot root, TimePeriod timePeriod)
        {
            IndicatorMetadata indicatorMetaData = GroupData.GetIndicatorMetadataById(root.IndicatorId);
            var benchmarkDataProvider = new BenchmarkDataProvider(GroupDataReader);

            // Comparator data
            foreach (Grouping grouping in root.Grouping)
            {
                Comparator comparator = ComparatorMap.GetComparatorById(grouping.ComparatorId, grouping.AreaTypeId);
                CoreDataSet data;
                if (comparator == null)
                {
                    data = CoreDataSet.GetNullObject(string.Empty);
                }
                else
                {
                    var comparatorArea = NearestNeighbourArea.IsNearestNeighbourAreaCode(comparator.Area.Code)
                         ? AreaFactory.NewArea(AreasReader, comparator.Area.Code.Substring(5))
                         : comparator.Area;

                    // Only get subnational data, do not want to prefetch England data as usually won't be needed
                    var dataList = grouping.ComparatorId == ComparatorIds.Subnational
                        ? root.Data
                        : null;

                    AverageCalculator averageCalculator = AverageCalculatorFactory.New(dataList, indicatorMetaData);

                    data = benchmarkDataProvider.GetBenchmarkData(grouping, timePeriod,
                            averageCalculator, comparatorArea);

                    // Calculate England data if not found in DB (this done after so do not have to read all data when necessary)
                    if (data.IsValueValid == false && grouping.ComparatorId == ComparatorIds.England)
                    {
                        dataList = GroupDataReader.GetCoreDataForAllAreasOfType(grouping, timePeriod);
                        averageCalculator = AverageCalculatorFactory.New(dataList, indicatorMetaData);
                        data = benchmarkDataProvider.GetBenchmarkData(grouping, timePeriod,
                            averageCalculator, comparatorArea);
                    }
                }
                grouping.ComparatorData = data;
            }
        }
        private IList<XyPoint> GetXYPointsForLsoaDeprivationDeciles(GroupRoot xRoot, GroupRoot yRoot)
        {
            const int categoryTypeId = CategoryTypeIds.LsoaDeprivationDecilesWithinArea2010;
            var xData = GetLsoaDeprivationDecilesWithinArea(xRoot, categoryTypeId);
            var yData = GetLsoaDeprivationDecilesWithinArea(yRoot, categoryTypeId);

            coreDataProcessor.TruncateList(xData);
            coreDataProcessor.TruncateList(yData);

            return new XyPointListBuilder(xData, yData).XyPoints;
        }
        private IList<CoreDataSet> GetLsoaDeprivationDecilesWithinArea(GroupRoot root, int categoryTypeId)
        {
            var grouping = root.FirstGrouping;
            var dataList = groupDataReader.GetCoreDataListForAllCategoryAreasOfCategoryAreaType(
                grouping, TimePeriod.GetDataPoint(grouping), categoryTypeId, areaCode);

            coreDataProcessor.TruncateList(dataList);

            return dataList;
        }
        private LocalAndEnglandChartDataWithDeprivation GetLocalAndEnglandChartDataWithDeprivation(GroupRoot groupRoot)
        {
            var grouping = groupRoot.FirstGrouping;

            var chartData = new LocalAndEnglandChartDataWithDeprivation(
                GetLocalAndEnglandChartData(groupRoot));

            chartData.LocalLeastDeprived = GetDeprivationQuintileValues(grouping,
                CategoryIds.LeastDeprivedQuintile);
            chartData.LocalMostDeprived = GetDeprivationQuintileValues(grouping,
                CategoryIds.MostDeprivedQuintile);

            return chartData;
        }
        private static Grouping GetGrouping(IArea parentArea, GroupRoot groupRoot)
        {
            Grouping grouping = parentArea.IsCountry
                ? groupRoot.GetNationalGrouping()
                : groupRoot.GetSubnationalGrouping();

            if (grouping == null)
            {
                throw new FingertipsException(
                    "Cannot find both national and subnational groupings for indicator ID " +
                    groupRoot.IndicatorId + ". You may need to change the comparator for this indicator in FPM.");
            }
            return grouping;
        }
 protected override void AddIndicatorData(GroupRoot groupRoot, IndicatorMetadata metadata, IList<Area> benchmarkAreas)
 {
     spineChartTableData.IndicatorData.Add(rowBuilder.GetIndicatorData(groupRoot, metadata, benchmarkAreas));
 }
 protected override void AddIndicatorData(GroupRoot groupRoot, IndicatorMetadata metadata, 
     IList<Area> benchmarkAreas)
 {
     domainNationalValues.IndicatorData.Add(
         builder.GetIndicatorData(groupRoot, metadata, benchmarkAreas));
 }
 private IList<CoreDataSet> GetChildAreaDataList(IArea parentArea,
     GroupRoot groupRoot, Grouping grouping, TimePeriod timePeriod)
 {
     IList<CoreDataSet> dataList;
     if (parentArea.IsCountry)
     {
         if (area.IsGpPractice)
         {
             // No need to calculate rank of practice in whole country
             throw new FingertipsException("Do not use for GP Practices");
         }
         else
         {
             // All data in country
             dataList = new CoreDataSetListProvider(groupDataReader).GetChildAreaData(grouping,
                 parentArea, timePeriod);
             dataList = new CoreDataSetFilter(dataList).RemoveWithAreaCode(ignoredAreaCodes).ToList();
         }
     }
     else
     {
         // Use subnational data
         dataList = groupRoot.Data;
     }
     return dataList;
 }
 public void WriteFile(GroupRoot groupRoot, IArea subnationalArea)
 {
     var bytes = _csvWriter.WriteAsBytes();
     var filename = @"c:\temp\" + groupRoot.IndicatorId + "-sex" + groupRoot.SexId + "-age" + groupRoot.AgeId + "-" + subnationalArea.ShortName + ".csv";
     System.IO.File.WriteAllText(filename, System.Text.Encoding.UTF8.GetString(bytes));
 }
 protected abstract void AddIndicatorData(GroupRoot groupRoot, IndicatorMetadata metadata, IList<Area> benchmarkAreas);
 private LocalAndEnglandChartData GetLocalAndEnglandChartData(GroupRoot groupRoot)
 {
     var grouping = groupRoot.FirstGrouping;
     var localAverageCoreDataSets = trendDataReader.GetTrendData(grouping, areaCode);
     var englandAverageCoreDataSets = trendDataReader.GetTrendData(grouping, benchmarkAreaCode);
     return new LocalAndEnglandChartData
     {
         Local = GetValues(localAverageCoreDataSets),
         England = GetValues(englandAverageCoreDataSets)
     };
 }
        private int GetSignificance(string areaCode, GroupRoot groupRoot, Grouping grouping,
            IList<CoreDataSet> childAreaDataList)
        {
            CoreDataSet areaData = AreaHelper.GetDataForAreaFromDataList(areaCode, groupRoot.Data);
            if (areaData == null)
            {
                return (int)Significance.None;
            }

            var comparer = indicatorComparerFactory.New(grouping);

            if (comparer is ICategoryComparer)
            {
                ICategoryComparer categoryComparer = comparer as ICategoryComparer;
                var values = new CoreDataSetFilter(childAreaDataList).SelectValidValues().ToList();
                categoryComparer.SetDataForCategories(values);

                return categoryComparer.GetCategory(areaData);
            }

            return areaData.Significance[grouping.ComparatorId];
        }