public void Truncate(IndicatorStatsPercentiles statsPercentiles)
 {
     if (statsPercentiles != null)
     {
         statsPercentiles.Max = RoundStats(statsPercentiles.Max);
         statsPercentiles.Min = RoundStats(statsPercentiles.Min);
         statsPercentiles.Percentile25 = RoundStats(statsPercentiles.Percentile25);
         statsPercentiles.Percentile75 = RoundStats(statsPercentiles.Percentile75);
     }
 }
 public override IndicatorStatsPercentilesFormatted FormatStats(IndicatorStatsPercentiles stats)
 {
     if (stats == null)
     {
         return GetNullStats();
     }
     return new IndicatorStatsPercentilesFormatted
     {
         Min = FormatNumber(stats.Min),
         Max = FormatNumber(stats.Max),
         Percentile25 = FormatNumber(stats.Percentile25),
         Percentile75 = FormatNumber(stats.Percentile75)
     };
 }
        public void TestPostProcessIndicatorStatsToLowerJsonFootPrint()
        {
            IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles
            {
                Min = 0.123456,
                Max = 1.111111111111,
                Percentile25 = 50,
                Percentile75 = 50.22222222222222
            };

            new IndicatorStatsProcessor().Truncate(statsPercentiles);

            Assert.AreEqual(0.12346, statsPercentiles.Min);
            Assert.AreEqual(1.11111, statsPercentiles.Max);
            Assert.AreEqual(50, statsPercentiles.Percentile25);
            Assert.AreEqual(50.22222, statsPercentiles.Percentile75);
        }
        public void TestFormatStats()
        {
            var formatter = NumericFormatterFactory.NewWithLimits(GetMetadata(1), null);
            var stats = new IndicatorStatsPercentiles
            {
                Min = 1.111111,
                Max = 2.222222,
                Percentile25 = 6.666666,
                Percentile75 = 7.777777
            };
            var statsF = formatter.FormatStats(stats);

            Assert.AreEqual("1.1", statsF.Min);
            Assert.AreEqual("2.2", statsF.Max);
            Assert.AreEqual("6.7", statsF.Percentile25);
            Assert.AreEqual("7.8", statsF.Percentile75);
        }
 public abstract IndicatorStatsPercentilesFormatted FormatStats(IndicatorStatsPercentiles stats);
        public void TestNullObjectFormattedCorrectlyIfStats3Dp()
        {
            IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles()
            {
                Min = 0.111,
                Max = 0.111,
                Percentile25 = 0,
                Percentile75 = 0
            };

            NumericFormatter formatter = NumericFormatterFactory.NewWithLimits(GetMetadata(), statsPercentiles);
            CoreDataSet data = CoreDataSet.GetNullObject(null);
            formatter.Format(data);
            Assert.AreEqual(NumericFormatter.NoValue, data.ValueFormatted);
        }
        private static void AssertValueAsExpected(IndicatorStatsPercentiles statsPercentiles, string expected, double val)
        {
            NumericFormatter formatter = NumericFormatterFactory.NewWithLimits(GetMetadata(), statsPercentiles);

            CoreDataSet data = new CoreDataSet
            {
                Value = val
            };
            formatter.Format(data);
            Assert.AreEqual(expected, data.ValueFormatted);
        }
        public void TestFormatProportionsLessThan10()
        {
            IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles
            {
                Max = 4.792910447761194
            };

            AssertValueAsExpected(statsPercentiles, "2.43", 2.432154729525536);
        }
        public void TestFormatProportionsCloseTo100()
        {
            IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles
            {
                Max = 100,
                Min = 0
            };

            AssertValueAsExpected(statsPercentiles, "100", 100);
            AssertValueAsExpected(statsPercentiles, "100", 99.9999999);
            AssertValueAsExpected(statsPercentiles, "100", 99.95);
            AssertValueAsExpected(statsPercentiles, "99.9", 99.94);
        }
        public void TestFormatIndicatorStatsMinMaxAtExpectedLimits()
        {
            IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles()
            {
                Min = 0,
                Max = 100,
                Percentile25 = 50,
                Percentile75 = 50
            };

            NumericFormatter formatter = NumericFormatterFactory.NewWithLimits(new IndicatorMetadata { ValueTypeId = (int)ValueTypeId.Proportion }, statsPercentiles);
            IndicatorStatsPercentilesFormatted statsFormatted = formatter.FormatStats(statsPercentiles);

            Assert.AreEqual("0.0", statsFormatted.Min);
            Assert.AreEqual("100", statsFormatted.Max);
        }
        public void TestFormatIndicatorStats()
        {
            IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles()
            {
                Min = 45.11,
                Max = 89.34,
                Percentile25 = 54,
                Percentile75 = 75.88888
            };

            NumericFormatter formatter = NumericFormatterFactory.NewWithLimits(new IndicatorMetadata { ValueTypeId = (int)ValueTypeId.Proportion }, statsPercentiles);
            IndicatorStatsPercentilesFormatted statsFormatted = formatter.FormatStats(statsPercentiles);

            Assert.AreEqual("45.1", statsFormatted.Min);
            Assert.AreEqual("89.3", statsFormatted.Max);
            Assert.AreEqual("54.0", statsFormatted.Percentile25);
            Assert.AreEqual("75.9", statsFormatted.Percentile75);
        }
        public List<SparklineRoot> BuildRoots(string parentAreaCode, int areaTypeId,
            int profileId, int groupId, IList<string> dataTypes, int sexId)
        {
            var groupIds = profileReader.GetProfile(profileId).GroupIds;

            IList<IArea> childAreas = areasReader
                .GetChildAreas(parentAreaCode, areaTypeId).Cast<IArea>().ToList();
            childAreas = IgnoredAreasFilterFactory.New(profileId).RemoveAreasIgnoredEverywhere(childAreas).ToList();

            ComparatorMap comparatorMap = new ComparatorMapBuilder(new ParentArea(parentAreaCode, areaTypeId)).ComparatorMap;
            var parentArea = AreaFactory.NewArea(areasReader,parentAreaCode);
            parentCoreDataSetProvider = new CoreDataSetProviderFactory().New(parentArea);
            var nationalArea = AreaFactory.NewArea(areasReader, AreaCodes.England);
            nationalCoreDataSetProvider = new CoreDataSetProviderFactory().New(nationalArea);

            // Get grouping for time info
            IList<int> indicatorIds = groupDataReader.GetIndicatorIdsByGroupIdAndAreaTypeId(groupId, areaTypeId);

            DoubleOverlappingCIsComparer comparer = new DoubleOverlappingCIsComparer();
            IndicatorMetadataRepository repository = IndicatorMetadataRepository.Instance;

            List<SparklineRoot> roots = new List<SparklineRoot>();

            foreach (var indicatorId in indicatorIds)
            {
                var grouping = GetGrouping(groupIds, indicatorId, areaTypeId, sexId);

                IndicatorMetadata metadata = repository.GetIndicatorMetadata(grouping);

                IList<TimePeriod> timePeriods = grouping.GetTimePeriodIterator(metadata.YearType).TimePeriods;

                SparklineRoot root = new SparklineRoot();
                roots.Add(root);

                List<ValueWithCIsData> allData = new List<ValueWithCIsData>();

                foreach (IArea area in childAreas)
                {
                    SparklineArea sparklineArea = new SparklineArea();
                    int startingAllDataCount = allData.Count;

                    foreach (TimePeriod timePeriod in timePeriods)
                    {
                        SparklineTimePoint timePoint = new SparklineTimePoint();
                        sparklineArea.TimePoints.Add(timePoint);

                        foreach (string dataType in dataTypes)
                        {
                            var data = GetData(area, dataType, grouping, timePeriod, allData);
                            timePoint.Data.Add(dataType, data);
                        }

                        // Are different? (comparatorId=10 “Within area 80/20 by deprivation”)
                        if (timePoint.Data.Count > 0)
                        {
                            ValueWithCIsData p1 = timePoint.Data[dataTypes[0]];
                            ValueWithCIsData p2 = timePoint.Data[dataTypes[1]];

                            Significance sig = comparer.Compare(p1, p2, metadata);
                            timePoint.AreDifferent = sig == Significance.Better || sig == Significance.Worse;
                        }
                    }

                    // Do not include areas with no data
                    if (allData.Count > startingAllDataCount)
                    {
                        root.AreaData.Add(area.Code, sparklineArea);
                    }
                }

                // Add statsPercentiles
                if (allData.Count > 0)
                {
                    IndicatorStatsPercentiles statsPercentiles = new IndicatorStatsPercentiles
                    {
                        Min = (from d in allData select d.LowerCI).Min<double>(),
                        Max = (from d in allData select d.UpperCI).Max<double>()
                    };

                    var builder = new TimePeriodTextListBuilder(metadata);
                    builder.AddRange(timePeriods);
                    IList<string> xLabels = builder.GetTimePeriodStrings();

                    SparklineStats sparklineStats = new SparklineStats(xLabels);
                    sparklineStats.IndicatorId = grouping.IndicatorId;
                    sparklineStats.Limits = new MinMaxRounder(statsPercentiles.Min, statsPercentiles.Max).Limits;
                    root.Stats = sparklineStats;

                    // Format
                    NumericFormatter formatter = NumericFormatterFactory.NewWithLimits(metadata, statsPercentiles);
                    new ValueWithCIsDataProcessor(formatter).FormatAndTruncateList(allData);

                    // Add comparator data
                    foreach (var comparator in comparatorMap.Comparators)
                    {
                        List<string> values = GetFormattedComparatorValues(timePeriods, metadata, grouping, formatter,
                            comparator.ComparatorId);
                        sparklineStats.ComparatorValues.Add(comparator.ComparatorId, values);
                    }
                }
            }

            return roots;
        }