public static void CreateMetaData(SpssDataDocument doc) { // Define dictionary SpssStringVariable v1 = new SpssStringVariable(); v1.Name = "v1"; v1.Label = "What is your name?"; doc.Variables.Add(v1); SpssNumericVariable v2 = new SpssNumericVariable(); v2.Name = "v2"; v2.Label = "How old are you?"; doc.Variables.Add(v2); SpssNumericVariable v3 = new SpssNumericVariable(); v3.Name = "v3"; v3.Label = "What is your gender?"; v3.ValueLabels.Add(1, "Male"); v3.ValueLabels.Add(2, "Female"); doc.Variables.Add(v3); SpssDateVariable v4 = new SpssDateVariable(); v4.Name = "v4"; v4.Label = "What is your birthdate?"; doc.Variables.Add(v4); // Add some data doc.CommitDictionary(); }
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(); } } } }
public void CreateDocument() { if (File.Exists(TestBase.DisposableFilename)) { File.Delete(TestBase.DisposableFilename); } try { using (SpssDataDocument doc = SpssDataDocument.Create(TestBase.DisposableFilename)) { Assert.Equal(SpssFileAccess.Create, doc.AccessMode); Assert.False(doc.IsClosed, "Newly opened document claims to be closed."); Assert.Equal(TestBase.DisposableFilename, doc.Filename); Assert.True(doc.IsAuthoringDictionary, "Newly created data file should be in dictionary authoring mode."); Assert.True(doc.IsCompressed, "Newly created documents should default to being Compressed."); } } finally { if (File.Exists(TestBase.DisposableFilename)) { File.Delete(TestBase.DisposableFilename); } } }
public static void PrintData(SpssDataDocument doc) { foreach (SpssVariable var in doc.Variables) { Console.Write(var.Name + Environment.NewLine); } Console.WriteLine(); foreach (SpssCase row in doc.Cases) { foreach (SpssVariable var in doc.Variables) { if ((row[var.Name] == null)) { Console.Write("<SYSMISS>"); } else { Console.Write(row[var.Name]); } Console.Write(Environment.NewLine); } Console.WriteLine(); } }
static void Main(string[] args) { Console.WriteLine("SPSS file writing demo:"); if (File.Exists("example.sav")) { File.Delete("example.sav"); } CreateExampleDocument(); Console.WriteLine("Exporting a DataTable demo... (the source code is interesting)"); DataTable dt = SpssConvert.ToDataTable(GetFileName()); if (File.Exists("example2.sav")) { File.Delete("example2.sav"); } SpssConvert.ToFile(dt, "example2.sav", MetaDataCallback); Console.WriteLine("SPSS dictionary copying demo:"); if (File.Exists("example3.sav")) { File.Delete("example3.sav"); } using (SpssDataDocument doc = SpssDataDocument.Create("example3.sav", GetFileName())) { PrintMetaData(doc); } Console.WriteLine("Demo concluded. Press any key to end."); Console.ReadKey(); }
public void Write(String filename) { _doc = SpssDataDocument.Create(filename); CreateColumnMetaData(_doc, _columnCodes); //_columnDefinitions); CreateRows(_doc, _journalAnswersCollection); Result = _doc; }
public void ReadNullValueLabels() { using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read)) { SpssVariable var = docRead.Variables[3]; Assert.False(var.GetValueLabels().Any()); } }
public void ReadNullLabel() { using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read)) { SpssVariable var = docRead.Variables[3]; Assert.Equal(string.Empty, var.Label); } }
public void ReadLabel() { using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read)) { SpssVariable var = docRead.Variables[0]; Assert.Equal("on butter", var.Label); } }
private void CreateRows(SpssDataDocument doc, IEnumerable <IAnswerBook> journalAnswersCollection) { Console.Out.WriteLine("journalsAnswers: {0}", journalAnswersCollection.Count()); foreach (var journalAnswers in journalAnswersCollection) { Console.Out.WriteLine("Pid key: {0} CreateRows: {1}", journalAnswers.Answers.First().Key, journalAnswers.Answers.Count); CreateJournalRows(doc, journalAnswers); } }
public void ReadDecimalPlaces() { using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read)) { SpssNumericVariable var = docRead.Variables[0] as SpssNumericVariable; Assert.NotNull(var); // First variable expected to be numeric. Assert.Equal(2, var.WriteDecimal); } }
public void GetLongStringValueLabels() { using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read)) { SpssStringVariable var = (SpssStringVariable)docRead.Variables["longStr"]; // long strings can never have value labels Assert.Equal(0, var.ValueLabels.Count); } }
public void SetStringTooLong() { using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append)) { SpssStringVariable var = (SpssStringVariable)docAppend.Variables["charLabels"]; Debug.Assert(var.Length == 8); SpssCase row = docAppend.Cases.New(); Assert.Throws <ArgumentOutOfRangeException>(() => row["charLabels"] = new string('a', var.Length + 1)); } }
public void OpenExistingDocumentForAppend() { using (SpssDataDocument doc = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append)) { Assert.Equal(SpssFileAccess.Append, doc.AccessMode); Assert.False(doc.IsClosed, "Newly opened document claims to be closed."); Assert.Equal(TestBase.AppendFilename, doc.Filename); Assert.False(doc.IsAuthoringDictionary, "Cannot be authoring dictionary when appending data."); } }
private static void CreateExampleDocument() { using (SpssDataDocument doc = SpssDataDocument.Create("example.sav")) { CreateMetaData(doc); CreateData(doc); } Console.WriteLine("Examine example.sav for the results."); Console.WriteLine("SPSS file reading demo:"); using (SpssDataDocument doc = SpssDataDocument.Open(GetFileName(), SpssFileAccess.Read)) { PrintMetaData(doc); PrintData(doc); } }
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); } }
public static void PrintMetaData(SpssDataDocument doc) { Console.WriteLine("Variables:"); foreach (SpssVariable var in doc.Variables) { Console.WriteLine("{0}" + Environment.NewLine + "{1}", var.Name, var.Label); if (var is SpssNumericVariable) { SpssNumericVariable varNum = (SpssNumericVariable)var; foreach (KeyValuePair <double, string> label in varNum.ValueLabels) { Console.WriteLine(Environment.NewLine + label.Key.ToString() + Environment.NewLine + label.Value.ToString()); } } } }
public static void CreateData(SpssDataDocument doc) { SpssCase case1 = doc.Cases.New(); case1.SetDBValue("v1", "Andrew"); case1.SetDBValue("v2", 24); case1.SetDBValue("v3", 1); case1.SetDBValue("v4", DateTime.Parse("1/1/1982 7:32 PM")); case1.Commit(); SpssCase case2 = doc.Cases.New(); case2.SetDBValue("v1", "Cindy"); case2.SetDBValue("v2", 21); case2.SetDBValue("v3", 2); case2.SetDBValue("v4", DateTime.Parse("12/31/2002")); case2.Commit(); }
public static void GenerateSPSSDocument(List <Test> tests, string fileName = "ALLdata.sav") { fileName = DataGenerator.DataDirectory + fileName; if (File.Exists(fileName)) { File.Delete(fileName); } using (SpssDataDocument doc = SpssDataDocument.Create(fileName)) { CreateMetaData(doc); foreach (var test in tests) { ParseTest(doc, test); } } }
public void SetMissingValueDateByNull() { int rowIndex; using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append)) { SpssCase row = docAppend.Cases.New(); rowIndex = row.Position; row["dateVar"] = null; row.Commit(); } using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Read)) { SpssCase row = docAppend.Cases[rowIndex]; DateTime?val = (DateTime?)row["dateVar"]; Assert.False(val.HasValue); } }
public void SetMissingValueNumeric() { int rowIndex; using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append)) { SpssCase row = docAppend.Cases.New(); rowIndex = row.Position; row["num"] = double.NaN; row.Commit(); } using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Read)) { SpssCase row = docAppend.Cases[rowIndex]; double val = (double)row["num"]; Assert.Equal(double.NaN, val); } }
public TestBase() { try { docRead = SpssDataDocument.Open(GoodFilename, SpssFileAccess.Read); docAppend = SpssDataDocument.Open(AppendFilename, SpssFileAccess.Append); if (File.Exists(DisposableFilename)) File.Delete(DisposableFilename); docWrite = SpssDataDocument.Create(DisposableFilename); } catch { docRead?.Dispose(); docAppend?.Dispose(); docWrite?.Dispose(); throw; } }
public TestBase() { try { docRead = SpssDataDocument.Open(GoodFilename, SpssFileAccess.Read); docAppend = SpssDataDocument.Open(AppendFilename, SpssFileAccess.Append); if (File.Exists(DisposableFilename)) { File.Delete(DisposableFilename); } docWrite = SpssDataDocument.Create(DisposableFilename); } catch { docRead?.Dispose(); docAppend?.Dispose(); docWrite?.Dispose(); throw; } }
private void CreateColumnMetaData(SpssDataDocument doc, IEnumerable <ICode> columnDefinitions) { var addedVariables = new Dictionary <String, bool>() { { "Id", true } }; foreach (var code in columnDefinitions) { var varName = code.Name; var label = string.IsNullOrEmpty(code.Label) ? code.Name : code.Label; var varDatatype = code.Datatype; if (!addedVariables.ContainsKey(varName)) { var v = CreateSpssVariable(varDatatype, varName, label, code.ColumnWidth); doc.Variables.Add(v); addedVariables.Add(varName, true); } } doc.CommitDictionary(); }
private void CreateJournalRows(SpssDataDocument doc, IAnswerBook journalAnswers) { var journalInfo = journalAnswers.AnswerMetadataList; var answers = journalAnswers.Answers.OrderBy(a => a.AnswerMetadata.Besvarelsesdato); // check if any of the answers is from the same survey //var lastAnswerDate = answers.First().JournalInfo.Besvarelsesdato; SpssCase case1 = doc.Cases.New(); // row. Can contain multiple answers. TODO: if more answers to the same survey, split answers into multiple rows bool isSetJournalData = false; foreach (var answer in answers) { //var answerDateDiff = answer.JournalInfo.Besvarelsesdato.Subtract(lastAnswerDate); //if(answerDateDiff.Days > 120) { // if more than 120 days difference between answers, we assume it's not the same followup, and thus a new row // case1.Commit(); // case1 = doc.Cases.New(); // isSetJournalData = false; //} int varIndex = 0; foreach (var variable in answer.Variables) { if (variable.Datatype == "Date") { var d = variable.Value.ToDate(); //variable.Value = string.Format("{0,2}{1,2}{2}", d.Day, d.Month, d.Year.ToString().Replace("20", "").Replace("19", "").ToInt()).Replace(" ", "0"); case1.SetDBValue(variable.Name, d); //variable.Value); continue; } if (variable.Name == "Pid") { //variable.Datatype = "String"; case1.SetDBValue(variable.Name, variable.Value); } if (variable.Datatype == "String" && variable.Value == "#NULL!") { variable.Value = ""; case1.SetDBValue(variable.Name, variable.Value); } if (variable.Datatype == "Numeric" && variable.Value == "" || variable.Value == "#NULL!") { variable.Value = "9"; case1.SetDBValue(variable.Name, variable.Value); } if (variable.Datatype != "Date" && variable.Value.StartsWith("0")) { variable.Value = "0.0"; } try { if (!isSetJournalData || varIndex > 9) // first 9 vars are journalInfo vars { case1.SetDBValue(variable.Name, variable.Value); } } catch (FormatException ex) { var message = string.Format("Variable name '{0}' could not set value: {1}. Label: {2} Answer: {3}. journalId: {4}", variable.Name, variable.Value, variable.Label, answer.Id, answer.Key); throw new FormatException(message, ex); } varIndex++; } isSetJournalData = true; } case1.Commit(); }
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(); }