Пример #1
0
        internal static SpssVariable LoadVariable(SpssVariablesCollection parent, string varName, int varType)
        {
            FormatTypeCode writeFormat, printFormat;
            int writeDecimal, writeWidth, printDecimal, printWidth;
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarWriteFormat(parent.Document.Handle, varName, out writeFormat, out writeDecimal, out writeWidth), "spssGetVarWriteFormat");
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarPrintFormat(parent.Document.Handle, varName, out printFormat, out printDecimal, out printWidth), "spssGetVarPrintFormat");

            SpssVariable variable;
            switch (varType)
            {
                case 0:
                    // This may be a date or a numeric
                    if (SpssDateVariable.IsDateVariable(writeFormat))
                        variable = new SpssDateVariable(parent, varName, writeFormat, writeWidth, printFormat, printWidth);
                    else
                        variable = new SpssNumericVariable(parent, varName, writeFormat, writeDecimal, writeWidth, printFormat, printDecimal, printWidth);
                    break;
                default:
                    Debug.Assert(varType == printWidth);
                    variable = new SpssStringVariable(parent, varName, varType);
                    break;
            }

            return variable;
        }
Пример #2
0
        internal static SpssVariable LoadVariable(SpssVariablesCollection parent, string varName, int varType)
        {
            FormatTypeCode writeFormat, printFormat;
            int            writeDecimal, writeWidth, printDecimal, printWidth;

            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarWriteFormat(parent.Document.Handle, varName, out writeFormat, out writeDecimal, out writeWidth), "spssGetVarWriteFormat");
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarPrintFormat(parent.Document.Handle, varName, out printFormat, out printDecimal, out printWidth), "spssGetVarPrintFormat");

            SpssVariable variable;

            switch (varType)
            {
            case 0:
                // This may be a date or a numeric
                if (SpssDateVariable.IsDateVariable(writeFormat))
                {
                    variable = new SpssDateVariable(parent, varName, writeFormat, writeWidth, printFormat, printWidth);
                }
                else
                {
                    variable = new SpssNumericVariable(parent, varName, writeFormat, writeDecimal, writeWidth, printFormat, printDecimal, printWidth);
                }

                break;

            default:
                Debug.Assert(varType == printWidth);
                variable = new SpssStringVariable(parent, varName, varType);
                break;
            }

            return(variable);
        }
Пример #3
0
        public override SpssVariable Clone()
        {
            SpssNumericVariable other = new SpssNumericVariable();

            this.CloneTo(other);
            return(other);
        }
Пример #4
0
        /// <summary>
        /// Defines the variables in the SPSS data file so that they mirror
        /// those defined in a <see cref="DataTable"/>.
        /// </summary>
        /// <param name="table">
        /// The DataTable whose list of columns are the ones we want to copy.
        /// </param>
        /// <param name="fillInMetadataCallback">
        /// The callback method to use to retrieve the additional metadata
        /// to put into the SPSS data document, that is not included in a DataTable.
        /// Optional.
        /// </param>
        public void ImportSchema(DataTable table, Action <SpssVariable> fillInMetadataCallback)
        {
            foreach (DataColumn column in table.Columns)
            {
                try
                {
                    SpssVariable var;
                    if (column.DataType == typeof(string))
                    {
                        var = new SpssStringVariable();
                        ((SpssStringVariable)var).Length = (column.MaxLength <0 || column.MaxLength> SpssSafeWrapper.SPSS_MAX_LONGSTRING) ? SpssSafeWrapper.SPSS_MAX_LONGSTRING : column.MaxLength;
                    }
                    else if (column.DataType == typeof(DateTime))
                    {
                        var = new SpssDateVariable();
                    }
                    else
                    {
                        var = new SpssNumericVariable();
                        if (column.DataType == typeof(float) || column.DataType == typeof(double))
                        {
                            ((SpssNumericVariable)var).PrintDecimal = 2;
                            ((SpssNumericVariable)var).WriteDecimal = 2;
                        }
                    }

                    var.Name = this.GenerateColumnName(column.ColumnName);
                    this.Add(var);

                    // Provide opportunity for callback function to fill in variable-specific metadata
                    if (fillInMetadataCallback != null)
                    {
                        try
                        {
                            fillInMetadataCallback(var);
                        }
                        catch (Exception ex)
                        {
                            throw new ApplicationException("Exception in metadata filler callback function on column " + column.ColumnName + ".", ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error adding column " + column.ColumnName + " schema information to the SPSS .SAV data file.", ex);
                }
            }
        }
Пример #5
0
        protected override void CloneTo(SpssVariable spssVar)
        {
            base.CloneTo(spssVar);
            SpssNumericVariable other = spssVar as SpssNumericVariable;

            if (other == null)
            {
                throw new ArgumentException("Must be of type " + GetType().Name + ".", "other");
            }
            other.PrintDecimal       = this.PrintDecimal;
            other.PrintFormat        = this.PrintFormat;
            other.PrintWidth         = this.PrintWidth;
            other.WriteDecimal       = this.WriteDecimal;
            other.WriteFormat        = this.WriteFormat;
            other.WriteWidth         = this.WriteWidth;
            other.MissingValueFormat = this.MissingValueFormat;
            other.MissingValues      = new List <double>(this.MissingValues);
            this.valueLabels.CopyTo(other.valueLabels);
        }
Пример #6
0
        public static void ReadSpssFile()
        {
            using (var doc = SpssDataDocument.Open("t.sav", SpssFileAccess.Append)) {
                SpssNumericVariable accuracy = new SpssNumericVariable {
                    Name = $"Accuracy",
                    Label = $"Distance in pixels from target",
                    MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT
                };
                doc.Variables.Add(accuracy);

                SpssStringVariable var = (SpssStringVariable)doc.Variables["someLabel"];

                SpssCase row = doc.Cases.New();
                row[$"Accuracy"] = new string('a', var.Length + 1);
            }
        }
Пример #7
0
        public static void CreateMetaData(SpssDataDocument doc, SpssFormat format = SpssFormat.Long)
        {
            SpssNumericVariable vID = new SpssNumericVariable();
            vID.Name = "UserID";
            vID.Label = "The user's ID";
            vID.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT;
            doc.Variables.Add(vID);

            if(format == SpssFormat.Long)
            {

                SpssNumericVariable attemptNumber = new SpssNumericVariable();
                attemptNumber.Name = $"AttemptNumber";
                attemptNumber.Label = $"The continuous number of this attempt";
                attemptNumber.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT;
                doc.Variables.Add(attemptNumber);

                SpssNumericVariable time = new SpssNumericVariable();
                time.Name = $"Efficiency";
                time.Label = $"Time taken in seconds for the attempt";
                time.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT;
                doc.Variables.Add(time);

                SpssStringVariable hit = new SpssStringVariable();
                hit.Name = $"Effectiveness";
                hit.Label = $"Whether the user hit the target or not";
                hit.ValueLabels.Add("Miss", "Miss");
                hit.ValueLabels.Add("Hit", "Hit");
                hit.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM;
                doc.Variables.Add(hit);

                SpssNumericVariable accuracy = new SpssNumericVariable();
                accuracy.Name = $"Accuracy";
                accuracy.Label = $"Distance in pixels from target";
                accuracy.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_RAT;
                doc.Variables.Add(accuracy);

                SpssStringVariable gridSize = new SpssStringVariable();
                gridSize.Name = $"TargetSize";
                gridSize.Label = $"Target (grid) size for attempt";
                gridSize.ValueLabels.Add("Small", "Small");
                gridSize.ValueLabels.Add("Large", "Large");
                gridSize.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM;
                doc.Variables.Add(gridSize);

                SpssStringVariable direction = new SpssStringVariable();
                direction.Name = $"Direction";
                direction.Label = $"Direction for attempt";
                direction.ValueLabels.Add("Push", "Push");
                direction.ValueLabels.Add("Pull", "Pull");
                direction.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM;
                doc.Variables.Add(direction);

                SpssStringVariable technique = new SpssStringVariable();
                technique.Name = $"Technique";
                technique.Label = $"The technique used for the attempt";
                technique.ValueLabels.Add("Pinch", "Pinch");
                technique.ValueLabels.Add("Swipe", "Swipe");
                technique.ValueLabels.Add("Throw", "Throw");
                technique.ValueLabels.Add("Tilt", "Tilt");
                technique.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM;
                doc.Variables.Add(technique);

                SpssStringVariable experiment = new SpssStringVariable();
                experiment.Name = $"Experiment";
                experiment.Label = $"The experiment in which the attempt was conducted in";
                // Target, Field, Old, Accuracy
                experiment.ValueLabels.Add("Target", "Target");
                experiment.ValueLabels.Add("Field", "Field");
                experiment.ValueLabels.Add("Old", "Old");
                experiment.ValueLabels.Add("Accuracy", "Accuracy");
                experiment.MeasurementLevel = MeasurementLevelCode.SPSS_MLVL_NOM;
                doc.Variables.Add(experiment);

            }

            doc.CommitDictionary();
        }
Пример #8
0
 public override SpssVariable Clone()
 {
     SpssNumericVariable other = new SpssNumericVariable();
     CloneTo(other);
     return other;
 }
Пример #9
0
 private SpssVariable CreateNumericVariable(Variable v)
 {
     SpssVariable var;
     SpssNumericVariable var1 = new SpssNumericVariable();
     foreach (var item in v.ValueLabels)
     {
         long key = long.Parse(item.Key);
         if (var1.ValueLabels.ContainsKey(key) == false)
             var1.ValueLabels.Add(key, item.Value);
     }
     var = var1;
     return var;
 }