Example #1
0
        void UpdateVariables(int rowindex)
        {
            if (updating)
            {
                return;
            }

            updating = true;

            var row = dtaGrid.Rows[rowindex];

            var boundvariables_values = new Dictionary <string, double>();
            var boundvariables_units  = new Dictionary <string, UnitsSI>();

            for (var i = 0; i < dtaGrid.ColumnCount; i++)
            {
                var variable =
                    (DataVariable)dtaGrid.Columns[i].Tag;

                var cell =
                    row.Cells[i].FormattedValue?.ToString();

                if (cell != null)
                {
                    if (variable.Type == VariableType.Dependent)
                    {
                        try {
                            var value = variable.Expression.Evaluate(boundvariables_values);
                            var units = variable.Expression.FindUnits(boundvariables_units);

                            row.Cells[i].Value = value.ToString("0.#####") + " " + units.ToString();
                        }
                        catch (KeyNotFoundException) {
                        }
                    }

                    var numexp = NumberExpression.Parse(ref cell);
                    if (numexp != null)
                    {
                        boundvariables_values.Add(variable.Name, numexp.Value);
                        boundvariables_units.Add(variable.Name, numexp.Units);
                    }
                }
            }

            updating = false;
        }
 static IExpression Parse_number(ref string src) =>
 NumberExpression.Parse(ref src);
Example #3
0
        void UpdateAverages()
        {
            if (updating)
            {
                return;
            }

            if (updating_averages)
            {
                return;
            }

            updating          = true;
            updating_averages = true;

            var dimensions =
                variables.Where(_ => _.Type == VariableType.Dimensional).ToArray();

            var measurements =
                variables.Where(_ => _.Type == VariableType.Independent).ToArray();

            var calculations =
                variables.Where(_ => _.Type == VariableType.Dependent).ToArray();

            var units = measurements.Concat(calculations).Select(_ => _.Units).ToArray();

            var kvps =
                dimensions.Select(_ => _.Values.Select(__ => new KeyValuePair <DataVariable, string>(_, __.Key))).ToArray();

            var i = 0;

            foreach (var ordinates in Combinations(kvps))
            {
                for (var j = ordinates.Length - 1; j >= 0; j--)
                {
                    var ordinate = ordinates[j];

                    if (ordinate.Key.BehavesLikeTrials)
                    {
                        bool shouldcollectvalues =
                            collectors.Contains(ordinate.Value);

                        if (shouldcollectvalues)
                        {
                            var k_skip =
                                j + 1 != ordinates.Length ?
                                Combinations(kvps.Skip(j + 1).ToArray()).Count() :
                                1;
                            var k_times = ordinate.Key.Values.Count(_ => _.Key.Any(char.IsDigit));
                            var k_start = i - k_skip * k_times;

                            var collection =
                                FlipDimensions(
                                    RangeInts(k_start, k_skip, k_times)
                                    .Select(
                                        k =>
                                        dtaGrid
                                        .Rows[k]
                                        .Cells
                                        .Cast <DataGridViewCell>()
                                        .Skip(dimensions.Length)
                                        .Select(
                                            cell =>
                                            cell.Value != null ?
                                            NumberExpression.Parse(cell.Value.ToString())?.Value.ToString() ??
                                            cell.Value.ToString() :
                                            null
                                            )
                                        //.Where(_ => _ != null)
                                        .ToArray()
                                        )
                                    .ToArray()
                                    )
                                .ToArray();

                            var collection_nums =
                                collection
                                .Select(
                                    column => (
                                        from x in column
                                        let k = NumberExpression.Parse(x)
                                                select
                                                k != null ?
                                                k.Value :
                                                0
                                        ).ToArray()
                                    )
                                .ToArray();

                            string[] answers = new string[measurements.Length + calculations.Length];

                            switch (ordinate.Value)
                            {
                            case "Avg":
                            case "Mean":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        var val = collection_nums[m].Average();
                                        //if (Math.Abs(val - 1.5) < 0.01) {
                                        //    System.Diagnostics.Debugger.Break();
                                        //}

                                        answers[m] = val.ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (IndexOutOfRangeException) {
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;

                            case "Median":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        var list = collection_nums[m].ToList();
                                        list.Sort();

                                        answers[m] = list[list.Count / 2].ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;

                            case "Mode":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    var freq = new Dictionary <string, int>();

                                    foreach (var item in collection[m])
                                    {
                                        if (freq.ContainsKey(item))
                                        {
                                            freq[item]++;
                                        }
                                        else
                                        {
                                            freq.Add(item, 1);
                                        }
                                    }

                                    var highestmode =
                                        freq.Values.Max();

                                    answers[m] = string.Join(";", freq.Keys.Where(key => freq[key] == highestmode));
                                    if (units[m] != "")
                                    {
                                        answers[m] += " " + units[m];
                                    }
                                }

                                break;

                            case "Range":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        answers[m] = (collection_nums[m].Max() - collection_nums[m].Min()).ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;

                            case "Min":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        answers[m] = collection_nums[m].Min().ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;

                            case "Max":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        answers[m] = collection_nums[m].Max().ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;

                            case "Mid":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        answers[m] = ((collection_nums[m].Max() + collection_nums[m].Min()) / 2f).ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;

                            case "StdDev":
                                for (int m = 0; m < answers.Length; m++)
                                {
                                    try {
                                        answers[m] = collection_nums[m].StandardDeviation().ToString("0.#####");
                                        if (units[m] != "")
                                        {
                                            answers[m] += " " + units[m];
                                        }
                                    }
                                    catch (InvalidOperationException) {
                                    }
                                }

                                break;
                            }

                            var row = dtaGrid.Rows[i];
                            for (int m = dimensions.Length; m < row.Cells.Count; m++)
                            {
                                row.Cells[m].Value = answers[m - dimensions.Length];
                            }
                        }
                    }
                }

                i++;
            }

            updating          = false;
            updating_averages = false;
        }