Skip to content

Commit cc18609

Browse files
committed
address #71
1 parent 1a9850d commit cc18609

File tree

11 files changed

+150
-27
lines changed

11 files changed

+150
-27
lines changed

Assets/Testing/AWS1HourTest.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ PrefabInstance:
12031203
- target: {fileID: 2834388984357925155, guid: 0a3b6392f04558844bd340e68ced1ff9,
12041204
type: 3}
12051205
propertyPath: m_AnchoredPosition.y
1206-
value: 1235
1206+
value: 1365
12071207
objectReference: {fileID: 0}
12081208
- target: {fileID: 2896031059644693069, guid: 0a3b6392f04558844bd340e68ced1ff9,
12091209
type: 3}
@@ -1253,7 +1253,7 @@ PrefabInstance:
12531253
- target: {fileID: 4267567203292169432, guid: 0a3b6392f04558844bd340e68ced1ff9,
12541254
type: 3}
12551255
propertyPath: m_AnchoredPosition.y
1256-
value: 2280
1256+
value: 2520
12571257
objectReference: {fileID: 0}
12581258
- target: {fileID: 4612929069887686480, guid: 0a3b6392f04558844bd340e68ced1ff9,
12591259
type: 3}

Assets/UXF/Scripts/Etc/MiniJSON.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
using System.Collections.Generic;
3434
using System.IO;
3535
using System.Text;
36+
using System.Globalization;
3637

3738
using UnityEngine;
3839

@@ -327,12 +328,12 @@ object ParseNumber()
327328
if (number.IndexOf('.') == -1)
328329
{
329330
long parsedLong;
330-
long.TryParse(number, out parsedLong);
331+
long.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedLong);
331332
return parsedLong;
332333
}
333334

334335
double parsedDouble;
335-
double.TryParse(number, out parsedDouble);
336+
double.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedDouble);
336337
return parsedDouble;
337338
}
338339

@@ -632,7 +633,7 @@ void SerializeOther(object value)
632633
|| value is float
633634
|| value is decimal)
634635
{
635-
builder.Append(Convert.ToDouble(value).ToString("R"));
636+
builder.Append(Convert.ToDouble(value).ToString("R", CultureInfo.InvariantCulture));
636637
}
637638
else if (value is int
638639
|| value is uint

Assets/UXF/Scripts/Etc/UXFDataTable.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Threading;
3+
using System.Globalization;
24
using System.Linq;
35
using System.Collections.Generic;
46

@@ -56,7 +58,7 @@ public void AddCompleteRow(UXFDataRow newRow)
5658
(newRow.Count == dict.Keys.Count);
5759

5860
if (!sameKeys)
59-
{
61+
{
6062
throw new InvalidOperationException(
6163
string.Format(
6264
"The row does not contain values for the same columns as the columns in the table!\nTable: {0}\nRow: {1}",
@@ -87,23 +89,44 @@ public int CountRows()
8789
/// <summary>
8890
/// Return the table as a set of strings, each string a line a row with comma-seperated values.
8991
/// </summary>
92+
/// <param name="formatProvider">Format provider (e.g. CultureInfo for decimal separator). Defaults to en-US.</param>
9093
/// <returns></returns>
91-
public string[] GetCSVLines()
94+
public string[] GetCSVLines(CultureInfo culture = null, string decimalFormat = "0.######")
9295
{
96+
culture = culture ?? Thread.CurrentThread.CurrentCulture;
9397
string[] headers = Headers;
9498
string[] lines = new string[CountRows() + 1];
95-
lines[0] = string.Join(",", headers);
99+
lines[0] = string.Join(culture.TextInfo.ListSeparator, headers);
96100
for (int i = 1; i < lines.Length; i++)
97101
{
98-
lines[i] = string.Join(",",
102+
lines[i] = string.Join(culture.TextInfo.ListSeparator,
99103
headers
100-
.Select(h => dict[h][i - 1].ToString().Replace(",", "_"))
104+
.Select(h => FormatItem(dict[h][i - 1], culture, decimalFormat))
101105
);
102106
}
103107

104108
return lines;
105109
}
106110

111+
static string FormatItem(object item, CultureInfo culture, string decimalFormat = "0.######")
112+
{
113+
switch (item)
114+
{
115+
case sbyte sbyteNum: return sbyteNum.ToString(culture);
116+
case byte byteNum: return byteNum.ToString(culture);
117+
case short shortNum: return shortNum.ToString(culture);
118+
case ushort ushortNum: return ushortNum.ToString(culture);
119+
case int intNum: return intNum.ToString(culture);
120+
case uint uintNum: return uintNum.ToString(culture);
121+
case long longNum: return longNum.ToString(culture);
122+
case ulong ulongNum: return ulongNum.ToString(culture);
123+
case float floatNum: return floatNum.ToString(decimalFormat, culture);
124+
case double doubleNum: return doubleNum.ToString(decimalFormat, culture);
125+
case decimal decimalNum: return decimalNum.ToString(decimalFormat, culture);
126+
default: return item.ToString().Replace(culture.TextInfo.ListSeparator, "_");
127+
};
128+
}
129+
107130
/// <summary>
108131
/// Return the table as a dictionary of lists.
109132
/// </summary>

Assets/UXF/Scripts/Trackers/MouseScreenTracker.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ protected override UXFDataRow GetCurrentValues()
3131
// get position and rotation
3232
Vector2 p = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
3333

34-
string format = "0";
3534

3635
// return position, rotation (x, y, z) as an array
3736
var values = new UXFDataRow()
3837
{
39-
("pix_x", p.x.ToString(format)),
40-
("pix_y", p.y.ToString(format))
38+
("pix_x", p.x),
39+
("pix_y", p.y)
4140
};
4241

4342
return values;

Assets/UXF/Scripts/Trackers/MouseWorldTracker.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@ protected override UXFDataRow GetCurrentValues()
3737
// get position and rotation
3838
Vector3 p = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCamera));
3939

40-
string format = "0.####";
41-
4240
// return position, rotation (x, y, z) as an array
4341
var values = new UXFDataRow()
4442
{
45-
("pos_x", p.x.ToString(format)),
46-
("pos_y", p.y.ToString(format)),
47-
("pos_z", p.z.ToString(format))
43+
("pos_x", p.x),
44+
("pos_y", p.y),
45+
("pos_z", p.z)
4846
};
4947

5048
return values;

Assets/UXF/Scripts/Trackers/PositionRotationTracker.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ protected override UXFDataRow GetCurrentValues()
3939
Vector3 p = gameObject.transform.position;
4040
Vector3 r = gameObject.transform.eulerAngles;
4141

42-
string format = "0.####";
43-
4442
// return position, rotation (x, y, z) as an array
4543
var values = new UXFDataRow()
4644
{
47-
("pos_x", p.x.ToString(format)),
48-
("pos_y", p.y.ToString(format)),
49-
("pos_z", p.z.ToString(format)),
50-
("rot_x", r.x.ToString(format)),
51-
("rot_y", r.y.ToString(format)),
52-
("rot_z", r.z.ToString(format))
45+
("pos_x", p.x),
46+
("pos_y", p.y),
47+
("pos_z", p.z),
48+
("rot_x", r.x),
49+
("rot_y", r.y),
50+
("rot_z", r.z)
5351
};
5452

5553
return values;

Assets/UXF/Tests/Editor/TestLocale.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System;
9+
using System.Threading;
10+
using System.Globalization;
11+
12+
namespace UXF.Tests
13+
{
14+
15+
public class TestLocale
16+
{
17+
[Test]
18+
public void TestUS(){ TestSingleLocale(new CultureInfo("en-US")); }
19+
20+
[Test]
21+
public void TestGB(){ TestSingleLocale(new CultureInfo("en-GB")); }
22+
23+
[Test]
24+
public void TestDE(){ TestSingleLocale(new CultureInfo("de-DE")); }
25+
26+
public void TestSingleLocale(CultureInfo culture)
27+
{
28+
// https://www.csharp-examples.net/culture-names/
29+
Thread.CurrentThread.CurrentCulture = culture;
30+
31+
(Session session, FileSaver fileSaver) = CreateSession(culture.DisplayName);
32+
session.blocks[0].trials[0].Begin();
33+
session.blocks[0].trials[0].result["number"] = 123.456f;
34+
session.blocks[0].trials[0].End();
35+
36+
string path = fileSaver.GetSessionPath(session.experimentName, session.ppid, session.number);
37+
session.End();
38+
GameObject.DestroyImmediate(session.gameObject);
39+
40+
// read the file to check data
41+
string[] lines = File.ReadAllLines(Path.Combine(path, "trial_results.csv"));
42+
var header = lines[0].Split(new string[]{culture.TextInfo.ListSeparator}, StringSplitOptions.None);
43+
int idx = Array.FindIndex(header, (v) => v == "number");
44+
45+
var firstRow = lines[1].Split(new string[]{culture.TextInfo.ListSeparator}, StringSplitOptions.None);
46+
string num = firstRow[idx];
47+
48+
Assert.AreEqual(123.456f.ToString(), num);
49+
50+
// json parsing never uses locale, as it does not allow commas within numbers
51+
var newObject = (Dictionary<string, object>) MiniJSON.Json.Deserialize("{ \"num\": 123.456 }");
52+
Assert.AreEqual(123.456d, newObject["num"]);
53+
}
54+
55+
Tuple<Session, FileSaver> CreateSession(string ppidExtra)
56+
{
57+
GameObject gameObject = new GameObject();
58+
FileSaver fileSaver = gameObject.AddComponent<FileSaver>();
59+
SessionLogger sessionLogger = gameObject.AddComponent<SessionLogger>();
60+
Session session = gameObject.AddComponent<Session>();
61+
fileSaver.storagePath = "example_output";
62+
63+
sessionLogger.AttachReferences(
64+
session
65+
);
66+
67+
session.dataHandlers = new DataHandler[] { fileSaver };
68+
69+
sessionLogger.Initialise();
70+
71+
fileSaver.verboseDebug = false;
72+
73+
string experimentName = "unit_test";
74+
string ppid = "test_locale_" + ppidExtra;
75+
session.Begin(experimentName, ppid);
76+
77+
// generate trials
78+
session.CreateBlock(2);
79+
session.CreateBlock(3);
80+
81+
return new Tuple<Session, FileSaver>(session, fileSaver);
82+
}
83+
84+
[TearDown]
85+
public void TearDown()
86+
{
87+
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
88+
}
89+
90+
}
91+
92+
}

Assets/UXF/Tests/Editor/TestLocale.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UXF/Tests/Editor/TestTrials.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public void WriteCommas()
121121
Assert.AreEqual(numHeaders, lines[0].Split(',').GetLength(0));
122122

123123
// trial 1 columns should equal header length
124+
Debug.Log( lines[1]);
124125
Assert.AreEqual(numHeaders, lines[1].Split(',').GetLength(0));
125126

126127
// trial 2 columns should equal header length

docs/wiki

Submodule wiki updated from 1ea2a63 to c5f6a5f

media/banner-2-0-small.png

461 KB
Loading

0 commit comments

Comments
 (0)