Beispiel #1
0
        static int CalculateNumberOfCopies(DataTable table, IndependentVariable <T> independentVariable)
        {
            int lowestCommonMultiple = LowestCommon.Multiple(table.Rows.Count, independentVariable.Values.Count);
            int numberOfCopies       = lowestCommonMultiple / table.Rows.Count;

            return(numberOfCopies);
        }
Beispiel #2
0
        /// <summary>
        /// Adds a variable to the collection of variables of the given type
        /// </summary>
        /// <typeparam name="T">The type matching the target collection of variables</typeparam>
        /// <param name="v">The variable to add to the collection of matching type</param>
        /// <returns>Returns true if the variable was successfully added</returns>
        public bool AddVariable <T>(T v)
        {
            Type varType = typeof(T);

            if (varType == typeof(IndependentVariable))
            {
                IndependentVariable iv = (IndependentVariable)Convert.ChangeType(v, typeof(IndependentVariable));
                if (!IndVars.ContainsKey(iv.Name))
                {
                    IndVars.Add(iv.Name, iv);
                }
                if (!AllVars.ContainsKey(iv.Name))
                {
                    AllVars.Add(iv.Name, iv);
                }
            }
            else
            {
                DependentVariable dv = (DependentVariable)Convert.ChangeType(v, typeof(DependentVariable));

                if (!DepVars.ContainsKey(dv.Name))
                {
                    DepVars.Add(dv.Name, dv);
                }
                if (!AllVars.ContainsKey(dv.Name))
                {
                    AllVars.Add(dv.Name, dv);
                }
            }
            return(true);
        }
Beispiel #3
0
        void AddValuesToTable(DataTable newTable, IndependentVariable <T> independentVariable)
        {
            T value = loopValues.FirstElement;

            foreach (DataRow newTableRow in newTable.Rows)
            {
                newTableRow[independentVariable.Name] = value;
                value = loopValues.NextElement;
            }
        }
 void CreateDistribution(IndependentVariable<T> independentVariable) {
     for (int i = 0; i < independentVariable.Probabilities.Count; i++) {
         float prob = independentVariable.Probabilities[i];
         T val = independentVariable.Values[i];
         int number = (int) (prob * 1000);
         for (int j = 0; j < number; j++) {
             distribution.Add(val);
         }
     }
 }
Beispiel #5
0
 /// <summary>
 /// Returns the value of the provided token.
 /// </summary>
 /// <param name="token">String of variables</param>
 /// <returns>Value of the provided token</returns>
 public int GetValue(string token)
 {
     if (char.IsDigit(token[0]))
     {
         // Return int converted value of token
         return(Convert.ToInt32(token));
     }
     else if (token[0] != '{')
     {
         IndependentVariable indVar = TryGetVariable <IndependentVariable>(token) as IndependentVariable;
         DependentVariable   depVar = TryGetVariable <DependentVariable>(token) as DependentVariable;
         if (indVar != null)
         {
             if (indVar.Value)
             {
                 return(1);
             }
             else
             {
                 return(0);
             }
         }
         else if (depVar != null)
         {
             if (depVar.Value)
             {
                 return(1);
             }
             else
             {
                 return(0);
             }
         }
         else
         {
             return(-1); // If variable doesn't exist
         }
     }
     else
     {
         // Create binary string builder
         StringBuilder binary = new StringBuilder();
         // For each variable in the token
         foreach (string var in GetVariables(token))
         {
             // Add variable's value to binary string
             binary.Append(GetValue(var));
         }
         // Return int converted binary string as value
         return(Convert.ToInt32(binary.ToString(), 2));
     }
 }
        /// <summary>
        /// Adds values of this variable to an empty table. Just one trial per value.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="independentVariable"></param>
        /// <returns></returns>
        public DataTable AddValuesToEmptyTable(DataTable table, IndependentVariable <T> independentVariable)
        {
            DataTable newTable = table.Clone();

            foreach (T value in independentVariable.Values)
            {
                DataRow newRow = newTable.NewRow();
                newRow[independentVariable.Name] = value;
                newTable.Rows.Add(newRow);
            }

            return(newTable);
        }
        /// <summary>
        /// Adds values of this variable to an empty table. Just one trial per value.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="independentVariable"></param>
        /// <returns></returns>
        public DataTable AddValuesToEmptyTable(DataTable table, IndependentVariable<T> independentVariable) {

            DataTable newTable = table.Clone();
            
            //Debug.Log("Adding rows to empty trialTable in variable creation");
            foreach (T value in independentVariable.Values) {
                DataRow newRow = newTable.NewRow();
                newRow[independentVariable.Name] = value;
                newTable.Rows.Add(newRow);
            }

            return newTable;

        }
        public override DataTable AddVariableValuesToTable(DataTable table, IndependentVariable<T> independentVariable) {

            table.CreateColumnForVariable(independentVariable);

            if (table.Rows.Count == 0) return AddValuesToEmptyTable(table, independentVariable);

            DataTable newTable = table.Copy();

            CreateDistribution(independentVariable);

            foreach (DataRow newTableRow in newTable.Rows) {
                newTableRow[independentVariable.Name] = distribution.RandomItem();
            }
            
            return newTable;
        }
Beispiel #9
0
        public override List <IObjectCodeElement> Parse()
        {
            List <IObjectCodeElement> output  = new List <IObjectCodeElement>();
            MatchCollection           matches = OutputRegex.Matches(Text);

            foreach (Match match in matches)
            {
                string token = match.Value;
                if (token == " ")
                {
                    output.Add(new SpaceFeed());
                }
                else if (token == "\n")
                {
                    // Output newline
                    output.Add(new LineFeed());
                }
                else if (token.Contains("("))
                {
                    output.Add(new Comment(token));
                }
                else if (token == "," || token == "{" || token == "}" || token == ":" || token == ")")
                {
                    output.Add(new Operator(token));
                }
                else
                {
                    IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(token) as IndependentVariable;
                    DependentVariable   depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(token) as DependentVariable;
                    if (indVar != null)
                    {
                        output.Add(indVar);
                    }
                    else if (depVar != null)
                    {
                        output.Add(depVar);
                    }
                }
            }

            // Output ending semicolon
            output.Add(new Operator(";"));
            // Output new line
            output.Add(new LineFeed());
            // Return output list
            return(output);
        }
Beispiel #10
0
        public override DataTable AddVariableValuesToTable(DataTable table, IndependentVariable <T> independentVariable)
        {
            table.CreateColumnForVariable(independentVariable);

            if (table.Rows.Count == 0)
            {
                return(AddValuesToEmptyTable(table, independentVariable));
            }

            DataTable newTable = table.Copy();

            foreach (DataRow newTableRow in newTable.Rows)
            {
                newTableRow[independentVariable.Name] = independentVariable.Values.RandomItem();
            }

            return(newTable);
        }
Beispiel #11
0
        public override DataTable AddVariableValuesToTable(DataTable table, IndependentVariable <T> independentVariable)
        {
            table.CreateColumnForVariable(independentVariable);
            if (table.Rows.Count == 0)
            {
                return(AddValuesToEmptyTable(table, independentVariable));
            }


            loopValues.AddRange(independentVariable.Values);
            int numberOfCopies = CalculateNumberOfCopies(table, independentVariable);

            DataTable newTable = MakeRequiredNumberOfCopiesOf(table, numberOfCopies);

            AddValuesToTable(newTable, independentVariable);

            return(newTable);
        }
Beispiel #12
0
        public BlockTable(List <Variable> allData, ExperimentDesign design)
        {
            //Get block Variables
            List <Variable> blockVariables = new List <Variable>();

            foreach (Variable datum in allData)
            {
                if (datum.TypeOfVariable == VariableType.Independent)
                {
                    IndependentVariable independentVariable = (IndependentVariable)datum;
                    if (independentVariable.Block)
                    {
                        blockVariables.Add(independentVariable);
                    }
                }
            }

            baseBlockTable = design.SortAndAddIVs(blockVariables, true);
        }
Beispiel #13
0
    private float[] FindRange(IndependentVariable iv)
    {
        var subRange = iv.max - iv.min;
        var numSteps = Mathf.CeilToInt(subRange / iv.step) + 1;
        var range    = new float[numSteps];

        for (var i = 0; i < numSteps; i++)
        {
            var num = i * iv.step + iv.min;
            if (num > iv.max)
            {
                range[i] = iv.max;
            }
            else
            {
                range[i] = num;
            }
        }
        return(range);
    }
Beispiel #14
0
        public override DataTable AddVariableValuesToTable(DataTable table, IndependentVariable <T> independentVariable)
        {
            table.CreateColumnForVariable(independentVariable);

            if (table.Rows.Count == 0)
            {
                return(AddValuesToEmptyTable(table, independentVariable));
            }

            DataTable newTable = table.Clone();

            foreach (DataRow tableRow in table.Rows)
            {
                foreach (T value in independentVariable.Values)
                {
                    newTable.ImportRow(tableRow);
                    DataRow newRow = newTable.Rows[newTable.Rows.Count - 1];
                    newRow[independentVariable.Name] = value;
                }
            }

            return(newTable);
        }
        /// <summary>
        /// Parses the text of this statement into a list of output elements.
        /// </summary>
        public override List <IObjectCodeElement> Parse()
        {
            // Create output list to return
            var output = new List <IObjectCodeElement>();
            // Get input and output seperator index
            int seperatorIndex = Text.IndexOf(':');
            // Start no contact index at 0
            int currentNoContactIndex = 0;
            // Get all output matches
            MatchCollection matches = OutputRegex.Matches(Text);

            // For each output match
            foreach (Match match in matches)
            {
                string token = match.Value;
                if (token == " ")
                {
                    output.Add(new SpaceFeed());
                }
                else if (token == "\n")
                {
                    // Output newline
                    output.Add(new LineFeed());
                }
                else if (token.Contains("("))
                {
                    output.Add(new Instantiation(token));
                }
                else if (token == "," || token == "{" || token == "}" || token == ":" || token == ")")
                {
                    output.Add(new Operator(token));
                }
                else
                {
                    if (match.Index > seperatorIndex && token == "NC")
                    {
                        output.Add(new DependentVariable(token, Instantiation.NoContactValues[currentNoContactIndex++]));
                    }
                    else
                    {
                        if (char.IsDigit(token[0]))
                        {
                            output.Add(new Constant(token));
                        }
                        else
                        {
                            IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(token) as IndependentVariable;
                            DependentVariable   depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(token) as DependentVariable;
                            if (indVar != null)
                            {
                                output.Add(indVar);
                            }
                            else if (depVar != null)
                            {
                                output.Add(depVar);
                            }
                        }
                    }
                }
            }

            // Output ending semicolon
            output.Add(new Operator(";"));
            // Output new line
            output.Add(new LineFeed());
            // Return output list
            return(output);
        }
Beispiel #16
0
        /// <summary>
        /// Parses the text of this statement into a list of output elements.
        /// </summary>
        public override List <IObjectCodeElement> Parse()
        {
            List <IObjectCodeElement> output = new List <IObjectCodeElement>();

            // Find format specifiers and extra spacing
            MatchCollection matches = OutputRegex.Matches(Text);

            foreach (Match match in matches)
            {
                string token = match.Value;
                if (token == " ")
                {
                    output.Add(new SpaceFeed());
                }
                else if (token == "\n")
                {
                    // Output newline
                    output.Add(new LineFeed());
                }
                else
                {
                    if (token[0] != '%')
                    {
                        if (char.IsDigit(token[0]))
                        {
                            output.Add(new Constant(token));
                        }
                        else
                        {
                            IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(token) as IndependentVariable;
                            DependentVariable   depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(token) as DependentVariable;
                            if (indVar != null)
                            {
                                output.Add(indVar);
                            }
                            else if (depVar != null)
                            {
                                output.Add(depVar);
                            }
                        }
                    }
                    else
                    {
                        bool     clickable    = true;
                        string   variableList = match.Groups["Vars"].Value;
                        string[] variables    = Parser.WhitespaceRegex.Split(variableList);

                        var binaryBuilder = new StringBuilder();
                        foreach (string variable in variables)
                        {
                            if (char.IsDigit(variable[0]))
                            {
                                binaryBuilder.Append(variable);

                                if (clickable)
                                {
                                    clickable = false;
                                }
                            }
                            else
                            {
                                IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(variable) as IndependentVariable;
                                DependentVariable   depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(variable) as DependentVariable;
                                if (indVar != null)
                                {
                                    if (indVar.Value)
                                    {
                                        binaryBuilder.Append(1);
                                    }
                                    else
                                    {
                                        binaryBuilder.Append(0);
                                    }
                                }
                                else
                                {
                                    if (depVar.Value)
                                    {
                                        binaryBuilder.Append(1);
                                    }
                                    else
                                    {
                                        binaryBuilder.Append(0);
                                    }

                                    if (clickable)
                                    {
                                        clickable = false;
                                    }
                                }
                            }
                        }

                        string binary = binaryBuilder.ToString();
                        char   format = char.ToUpper(match.Groups["Format"].Value[0]);

                        string formatOutput     = format == 'B' ? binary : Format(format, binary);
                        string formatNextOutput = clickable ? GetNextValue(binary) : null;

                        int outputWidth = format == 'B' ? formatOutput.Length : GetWidth(format, variables.Length);
                        if (formatOutput.Length < outputWidth)
                        {
                            if (format == 'H')
                            {
                                formatOutput = string.Concat(new string('0', outputWidth - formatOutput.Length), formatOutput);
                            }
                            else
                            {
                                for (int i = 0; i < outputWidth - formatOutput.Length; i++)
                                {
                                    output.Add(new SpaceFeed());
                                }
                            }
                        }
                        output.Add(new Formatter(formatOutput, $"{{{variableList}", formatNextOutput));
                    }
                }
            }

            // Output ending semicolon
            output.Add(new Operator(";"));
            // Output new line
            output.Add(new LineFeed());
            // Return output list
            return(output);
        }
 /// <summary>
 /// Adds a variable's values to a data table.
 /// </summary>
 /// <param name="table"></param>
 /// <param name="independentVariable"></param>
 /// <returns></returns>
 public abstract DataTable AddVariableValuesToTable(DataTable table, IndependentVariable<T> independentVariable);
Beispiel #18
0
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        EditorGUILayout.HelpBox("This Design file was created using an older version. Please Update it now. You may lose some settings", MessageType.Warning);
        ExperimentDesignFile old = target as ExperimentDesignFile;

        if (old == null)
        {
            throw new NullReferenceException("Can't retrieve old experiment design file");
        }
        if (GUILayout.Button("Update now"))
        {
            ExperimentDesignFile2 newFile = CreateDesignFile();
            newFile.Factory.IndependentVariables = old.Factory.IndependentVariables;
            newFile.Factory.DependentVariables   = old.Factory.DependentVariables;
            newFile.Factory.ParticipantVariables = old.Factory.ParticipantVariables;

            newFile.ControlSettings      = old.ControlSettings;
            newFile.GuiSettings          = old.GuiSettings;
            newFile.ColumnNamesSettings  = old.ColumnNamesSettings;
            newFile.FileLocationSettings = old.FileLocationSettings;

            newFile.TrialRandomization = old.TrialRandomization;

            newFile.TrialRepetitions                 = old.TrialRepetitions;
            newFile.TrialPermutationType             = old.TrialPermutationType;
            newFile.BlockRandomization               = old.BlockRandomization;
            newFile.BlockPartialRandomizationSubType = old.BlockPartialRandomizationSubType;

            newFile.ExperimentRepetitions = old.ExperimentRepetitions;

            foreach (Variable variable in newFile.Factory.ParticipantVariables)
            {
                ParticipantVariable participantVariable = variable as ParticipantVariable;
                participantVariable.DataType = ConvertOldDataType(participantVariable.DataType);
                participantVariable.ConvertOldValues();
            }
        }



        EditorGUILayout.LabelField("IVs:");
        EditorGUI.indentLevel++;
        foreach (Variable variable in old.Factory.IndependentVariables)
        {
            EditorGUILayout.LabelField($"{variable.Name}, type {variable.DataType}");
            IndependentVariable iv     = variable as IndependentVariable;
            List <string>       values = iv.ValuesAsString();
            EditorGUI.indentLevel++;
            foreach (string value in values)
            {
                EditorGUILayout.LabelField(value);
            }
            EditorGUI.indentLevel--;
        }
        EditorGUI.indentLevel--;

        EditorGUILayout.LabelField("DVs:");
        EditorGUI.indentLevel++;
        foreach (Variable variable in old.Factory.DependentVariables)
        {
            EditorGUILayout.LabelField($"{variable.Name}, type {variable.DataType}");
        }
        EditorGUI.indentLevel--;

        EditorGUILayout.LabelField("PVs:");
        EditorGUI.indentLevel++;
        foreach (Variable variable in old.Factory.ParticipantVariables)
        {
            EditorGUILayout.LabelField($"{variable.Name}, type {ConvertOldDataType(variable.DataType)}");
        }
        EditorGUI.indentLevel--;

        serializedObject.ApplyModifiedProperties();
    }
Beispiel #19
0
        /// <summary>
        /// Parses the text of this statement into a list of output elements.
        /// </summary>
        public override List <IObjectCodeElement> Parse()
        {
            // Create output list to return
            var             output  = new List <IObjectCodeElement>();
            MatchCollection matches = OutputRegex.Matches(Text);

            foreach (Match match in matches)
            {
                string token = match.Value;

                if (token == " ")
                {
                    output.Add(new SpaceFeed());
                }
                else if (token == "\n")
                {
                    // Output newline
                    output.Add(new LineFeed());
                }
                else if (token == "(" || token == ")")
                {
                    output.Add(Expression.Parentheses[match.Index]); // Output the corresponding parenthesis
                }
                else if (Parser.OperatorsList.Contains(token) || token == "{" || token == "}" || token == "=")
                {
                    output.Add(new Operator(token));
                }
                else
                {
                    string name = token.TrimStart('~');
                    if (!char.IsDigit(name[0]))
                    {
                        IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(name) as IndependentVariable;
                        DependentVariable   depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(name) as DependentVariable;
                        if (indVar != null)
                        {
                            if (token[0] != '~')
                            {
                                output.Add(indVar);
                            }
                            else
                            {
                                output.Add(new IndependentVariable(token, !indVar.Value));
                            }
                        }
                        else if (depVar != null)
                        {
                            if (token[0] != '~')
                            {
                                output.Add(depVar);
                            }
                            else
                            {
                                output.Add(new DependentVariable(token, !depVar.Value));
                            }
                        }
                    }
                    else
                    {
                        output.Add(new Constant(token));
                    }
                }
            }

            // Output ending semicolon
            output.Add(new Operator(";"));
            // Output new line
            output.Add(new LineFeed());
            // Return output list
            return(output);
        }
        //Sort Independent variables into mixing categories so they go in order
        public SortedVariableContainer(List <Variable> allData, bool block)
        {
            int numberOfBlockIvs    = 0;
            int numberOfNonBlockIvs = 0;

            foreach (Variable variable in allData)
            {
                if (variable.TypeOfVariable == VariableType.Independent)
                {
                    IndependentVariable independentVariable = (IndependentVariable)variable;

                    if (independentVariable.Block)
                    {
                        numberOfBlockIvs++;
                    }
                    else
                    {
                        numberOfNonBlockIvs++;
                    }

                    if (block && independentVariable.Block || !block && !independentVariable.Block)
                    {
                        switch (independentVariable.MixingTypeOfVariable)
                        {
                        case VariableMixingType.Balanced:
                            BalancedIndependentVariables.Add(independentVariable);
                            break;

                        case VariableMixingType.Looped:
                            LoopedIndependentVariables.Add(independentVariable);
                            break;

                        case VariableMixingType.EvenProbability:
                        case VariableMixingType.CustomProbability:
                            ProbabilityIndependentVariables.Add(independentVariable);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
                else if (variable.TypeOfVariable == VariableType.Dependent)
                {
                    DependentVariable dependentVariable = (DependentVariable)variable;
                    DependentVariables.Add(dependentVariable);
                }
                else if (variable.TypeOfVariable == VariableType.Participant)
                {
                    ParticipantVariable participantVariable = (ParticipantVariable)variable;
                    ParticipantVariables.Add(participantVariable);
                }
            }

            bool thereAreBlockIvsButNoNormalIvs = numberOfBlockIvs > 0 && numberOfNonBlockIvs == 0;

            if (!block && thereAreBlockIvsButNoNormalIvs)
            {
                throw new ExperimentDesign.InvalidExperimentDesignException($"You defined {numberOfBlockIvs} block variable(s), " +
                                                                            $"when there are {numberOfNonBlockIvs} unblocked independent variables." +
                                                                            $"You can safely unblock the variable " +
                                                                            $"to make it a normal variable");
            }
        }