예제 #1
0
        /// <summary>
        /// Creates a new SPSS data document, initializing its dictionary
        /// by copying the dictionary from an existing SPSS data file.
        /// </summary>
        /// <param name="filename">
        /// The filename of the new document to create.
        /// </param>
        /// <param name="copyDictionaryFromFileName">
        /// The filename of the existing SPSS data file to copy the dictionary from.
        /// </param>
        /// <returns>
        /// The newly created <see cref="SpssDataDocument">SPSS data document</see>.
        /// </returns>
        public static SpssDataDocument Create(string filename, string copyDictionaryFromFileName)
        {
            if (File.Exists(filename))
            {
                throw new InvalidOperationException("File to create already exists.");
            }

            if (!File.Exists(copyDictionaryFromFileName))
            {
                throw new FileNotFoundException("File to copy does not exist.", copyDictionaryFromFileName);
            }

            using (SpssDataDocument read = SpssDataDocument.Open(copyDictionaryFromFileName, SpssFileAccess.Read))
            {
                SpssDataDocument toReturn = new SpssDataDocument(filename, SpssFileAccess.Create);

                foreach (SpssVariable var in read.Variables)
                {
                    toReturn.Variables.Add(var.Clone());
                }

                toReturn.CommitDictionary();
                return(toReturn);
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssCasesCollection"/> class.
        /// </summary>
        /// <param name="document">The hosting document.</param>
        internal SpssCasesCollection(SpssDataDocument document)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            this.Document = document;
        }
예제 #3
0
        public static void ToDataTable(SpssDataDocument doc, DataTable dataTable)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }

            if (dataTable == null)
            {
                throw new ArgumentNullException("dataTable");
            }

            // Build initial DataTable up.
            // Fill in the metadata.
            // set up the columns with the metadata
            foreach (SpssVariable var in doc.Variables)
            {
                string nameOfVar = var.Name;

                // add a column of the variable name to the DataTable
                DataColumn dataColumn = dataTable.Columns.Add(nameOfVar);

                // label of the variable is set in "varlabel" and extracted using var.Label
                dataColumn.Caption = var.Label;

                // set the type of the column
                if (var is SpssNumericVariable)
                {
                    dataColumn.DataType = typeof(double);
                }
                else if (var is SpssStringVariable)
                {
                    dataColumn.DataType = typeof(string);
                }
                else if (var is SpssDateVariable)
                {
                    dataColumn.DataType = typeof(DateTime);
                }
                else
                {
                    throw new NotSupportedException("Variable " + nameOfVar + " is not a string or a numeric variable type.");
                }
            }// end of extraction of metadata

            // add data into the DataTable
            var values = new object[doc.Variables.Count];

            foreach (SpssCase rowCase in doc.Cases)
            {
                for (int i = 0; i < doc.Variables.Count; i++)
                {
                    values[i] = rowCase[i];
                }

                dataTable.Rows.Add(values);
            }
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssCasesCollection"/> class.
        /// </summary>
        /// <param name="document">The hosting document.</param>
        internal SpssCasesCollection(SpssDataDocument document)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            this.Document = document;
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssVariablesCollection"/> class.
        /// </summary>
        /// <param name="document">The hosting SPSS data document whose variables will be managed by
        /// this instance.</param>
        internal SpssVariablesCollection(SpssDataDocument document)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            this.Document = document;

            this.InitializeVariablesList();
        }
예제 #6
0
        /// <summary>
        /// Converts the metadata in an SPSS .SAV data file into a DataTable.
        /// </summary>
        /// <param name="spssSavFilename">
        /// The filename of the SPSS .SAV data file.
        /// </param>
        /// <returns>
        /// The <see cref="DataTable"/> containing all the metadata.
        /// </returns>
        public static DataTable ToDataTable(string spssSavFilename)
        {
            if (spssSavFilename == null)
            {
                throw new ArgumentNullException("spssSavFilename");
            }
            DataTable dataTable = new DataTable();

            using (SpssDataDocument doc = SpssDataDocument.Open(spssSavFilename, SpssFileAccess.Read))
            {
                ToDataTable(doc, dataTable);
            }

            // Return the completed DataTable.
            return(dataTable);
        }
예제 #7
0
 /// <summary>
 /// Call to convert data to SPSS format using a passed in SQL query to provide the data.
 /// </summary>
 /// <param name="dataTable">The DataTable to convert to SPSS format</param>
 /// <param name="data">An enumerable list of DataRows.</param>
 /// <param name="spssSavFilename">The fully-qualified target .SAV file to save results to</param>
 /// <param name="fillInMetaDataCallBack">Callback function to provide per-variable metadata</param>
 public static void ToFile(DataTable dataTable, IEnumerable <DataRow> data,
                           string spssSavFilename, Action <SpssVariable> fillInMetaDataCallBack)
 {
     // Remove the file if it already exists.
     if (File.Exists(spssSavFilename))
     {
         File.Delete(spssSavFilename);
     }
     // Open up the document with "using" so that it will definitely close afterward.
     using (SpssDataDocument Sav = SpssDataDocument.Create(spssSavFilename))
     {
         // Create the schema from the table, passing in a callback
         // function for filling in each variable's metadata
         Sav.Variables.ImportSchema(dataTable, fillInMetaDataCallBack);
         // Import data
         Sav.CommitDictionary();
         Sav.ImportData(dataTable, data);
     }
 }
예제 #8
0
        /// <summary>
        /// Creates a new SPSS data document, initializing its dictionary 
        /// by copying the dictionary from an existing SPSS data file.
        /// </summary>
        /// <param name="filename">
        /// The filename of the new document to create.
        /// </param>
        /// <param name="copyDictionaryFromFileName">
        /// The filename of the existing SPSS data file to copy the dictionary from.
        /// </param>
        /// <returns>
        /// The newly created <see cref="SpssDataDocument">SPSS data document</see>.
        /// </returns>
        public static SpssDataDocument Create(string filename, string copyDictionaryFromFileName)
        {
            if (File.Exists(filename))
            {
                throw new InvalidOperationException("File to create already exists.");
            }
            if (!File.Exists(copyDictionaryFromFileName))
            {
                throw new FileNotFoundException("File to copy does not exist.", copyDictionaryFromFileName);
            }
            using (SpssDataDocument read = SpssDataDocument.Open(copyDictionaryFromFileName, SpssFileAccess.Read))
            {
                SpssDataDocument toReturn = new SpssDataDocument(filename, SpssFileAccess.Create);

                foreach (SpssVariable var in read.Variables)
                {
                    toReturn.Variables.Add(var.Clone());
                }

                toReturn.CommitDictionary();
                return toReturn;
            }
        }
예제 #9
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();
        }
예제 #10
0
        public static void ParseTest(SpssDataDocument doc, Test test, SpssFormat format = SpssFormat.Long)
        {
            int id = int.Parse(test.ID);
            //int overallAttempt = 0;

            if (format == SpssFormat.Long)
            {

                //UserID, AttemptNumber, Efficiency, Effectiveness, Accuracy, TargetSize, Direction, Technique, Experiment
                foreach (var type in AllTechniques) {

                    foreach(var attempt in test.Attempts[type]) {
                        SpssCase gestureAttempts = doc.Cases.New();
                        gestureAttempts[$"UserID"] = id;
                        gestureAttempts[$"AttemptNumber"] = attempt.AttemptNumber;
                        gestureAttempts[$"Efficiency"] = attempt.Time.TotalSeconds;
                        gestureAttempts[$"Effectiveness"] = attempt.Hit ? "Hit" : "Miss";
                        gestureAttempts[$"Accuracy"] = MathHelper.GetDistance(attempt);
                        gestureAttempts[$"TargetSize"] = attempt.Size.ToString().UppercaseFirst();
                        gestureAttempts[$"Direction"] = attempt.Direction.ToString().UppercaseFirst();
                        gestureAttempts[$"Technique"] = attempt.Type.ToString().UppercaseFirst();
                        gestureAttempts[$"Experiment"] = attempt.Source.ToString().UppercaseFirst();
                        gestureAttempts.Commit();

                    }
                }
            }
        }
예제 #11
0
        /// <summary>
        /// Converts the metadata in an SPSS .SAV data file into a DDI codebook.
        /// </summary>
        /// <param name="spssSavFilename">
        /// The filename of the SPSS .SAV data file.
        /// </param>
        /// <returns>
        /// The <see cref="XmlDocument"/> containing all the metadata.
        /// </returns>
        public static XmlDocument ToDdi(string spssSavFilename)
        {
            const string ddiNamespace = "http://www.icpsr.umich.edu/DDI";

            if (spssSavFilename == null)
            {
                throw new ArgumentNullException("spssSavFilename");
            }

            XmlDocument ddi = new XmlDocument();

            // Build initial ddi document up.
            // Open up SPSS file and fill in the ddi var tags.
            using (SpssDataDocument doc = SpssDataDocument.Open(spssSavFilename, SpssFileAccess.Read))
            {
                ddi.PreserveWhitespace = true;

                // Read from the embedded xml file: blankDdi.xml into the ddi document
                ddi.LoadXml(EmbeddedResources.LoadFileFromAssemblyWithNamespace("/blankDdi.xml", Project.DefaultNamespace));

                // This is where the hard coding ends and methods are called to extract data from the sav file
                XmlElement          xmlRoot = ddi.DocumentElement;
                XmlNamespaceManager xmlNS   = new XmlNamespaceManager(ddi.NameTable);
                xmlNS.AddNamespace("ddi", ddiNamespace);
                XmlNode nData = xmlRoot.SelectSingleNode(@"ddi:dataDscr", xmlNS);

                foreach (SpssVariable var in doc.Variables)
                {
                    string nameOfVar = var.Name;

                    // variable name and its ID and then if its a numeric : its interval
                    XmlElement variable = ddi.CreateElement("ddi:var", ddiNamespace);
                    variable.SetAttribute("ID", string.Empty, nameOfVar);
                    variable.SetAttribute("name", string.Empty, nameOfVar);

                    // This is the variable that holds the characteristic whether the variable has discrete or continuous interval
                    int Dec;
                    if (var is SpssNumericVariable)
                    {
                        Dec = ((SpssNumericVariable)var).PrintDecimal;
                        string interval = string.Empty;
                        if (Dec == 0)
                        {
                            interval = "discrete";
                        }
                        else
                        {
                            interval = "contin";
                        }

                        variable.SetAttribute("intrvl", string.Empty, interval);
                    }

                    // for the location width part
                    XmlElement location = ddi.CreateElement("ddi:location", ddiNamespace);
                    int        Wid      = var.ColumnWidth;
                    location.SetAttribute("width", Wid.ToString());
                    variable.AppendChild(location);

                    // label of the variable is set in "varlabel" and extracted using var.Label
                    XmlElement varLabel = ddi.CreateElement("ddi:labl", ddiNamespace);
                    varLabel.InnerText = var.Label;
                    variable.AppendChild(varLabel);

                    foreach (var response in var.GetValueLabels())
                    {
                        XmlElement answer = ddi.CreateElement("ddi:catgry", ddiNamespace);

                        // catValue(category Value) is the element storing the text i.e. option number
                        XmlElement catValue = ddi.CreateElement("ddi:catValu", ddiNamespace);
                        catValue.InnerText = response.Key;
                        answer.AppendChild(catValue);

                        // catLabel(category Label) is the element storing the text i.e. name of answer
                        XmlElement catLabel = ddi.CreateElement("ddi:labl", ddiNamespace);
                        catLabel.InnerText = response.Value;
                        answer.AppendChild(catLabel);

                        // appending the answer option to the parent "variable" node i.e. the question node
                        variable.AppendChild(answer);
                    }

                    // end of extracting the response values for each variable
                    XmlElement varFormat = ddi.CreateElement("ddi:varFormat", ddiNamespace);

                    if (var is SpssNumericVariable)
                    {
                        varFormat.SetAttribute("type", "numeric");
                    }
                    else if (var is SpssStringVariable)
                    {
                        varFormat.SetAttribute("type", "character");
                    }
                    else
                    {
                        throw new NotSupportedException("Variable " + nameOfVar + " is not a string or a numeric variable type.");
                    }

                    variable.AppendChild(varFormat);

                    nData.AppendChild(variable);
                }

                // end of extraction of each variable and now we have put all the variable data into ndata
                // Return the completed ddi file.
                return(ddi);
            }
        }
예제 #12
0
 private void CreateMetaData(List<Variable> list, SpssDataDocument doc)
 {
     foreach (Variable v in list)
     {
         SpssVariable var = CreateVariable(v);
         doc.Variables.Add(var);
     }
     doc.CommitDictionary();
 }
예제 #13
0
 private void CreateIdVariables(Entity parent, Entity e, SpssDataDocument doc)
 {
     var var = CreateVariable(new Variable(MakeId(e), "INTEGER", "Identifier for " + e.Name));
     doc.Variables.Add(var);
     if (parent != null)
     {
         var = CreateVariable(new Variable(MakeId(parent), "INTEGER", "Parent entity Id"));
         doc.Variables.Add(var);
     }
 }
예제 #14
0
        private void CreateData(Entity e, Entity parentEntity, SpssDataDocument doc)
        {
            // calcula columnas de ids
            string id = MakeId(e);
            string parentId = MakeId(parentEntity);
            // abre los archivos para todas las variables
            OpenVariablesData(e);
            //
            this.CurrentEntityTotal = e.GetPointerRows();

            int currentParent = -1;
            int parentLimit = int.MinValue;
            int n = 1;
            EntityCurrentRow = 0;
            // pone valores
            while (HasData(e))
            {
                SpssCase case1 = doc.Cases.New();
                // pone su id
                case1[id] = n;
                //ResolveParentId(parentEntity, parentId, ref currentParent, ref parentLimit, n, case1);
                ResolveParentId(e, parentId, ref currentParent, ref parentLimit, n, case1);
                CopyVariablesData(e, case1);
                case1.Commit();
                GlobalCurrentRow++;
                EntityCurrentRow++;

                if (GlobalCurrentRow % 43 == 0)
                {
                    CallBack(this, null);
                    if (Cancelled)
                    {
                        CloseVariablesData(e, parentEntity);
                        throw new Exception("Cancelled.");
                    }
                }
                n++;
            }

            CloseVariablesData(e, parentEntity);
        }