Skip to content

Commit 61f8070

Browse files
authored
Merge pull request #44 from ogeesan/fix/liquidcal-startup-errors
Improve handling for example calibration data on fresh setup
2 parents 41da296 + 98dea78 commit 61f8070

File tree

7 files changed

+49
-36
lines changed

7 files changed

+49
-36
lines changed

Examples/Example Calibration Files/LiquidCalibration.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"metadata": {
3-
"modification_datetime": "2000-01-01T00:00:00"
3+
"modification_datetime": "2000-01-01T00:00:00",
4+
"COM": "example data"
45
},
56
"ValveDatas": [
67
{

Functions/+BpodLib/+BpodObject/+setup/updatePathAndSettings.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ function updatePathAndSettings(BpodSystem, varargin)
103103
%% -- Calibration Files
104104
calFolder = BpodLib.path.getPath('calibration', BpodSystem, 'LocalDir', LocalDir);
105105

106-
if ~isfile(fullfile(Path.SettingsDir, 'LiquidCalibration.json')) && ~BpodLib.path.compatibility.isLegacySettings(Path.LocalDir)
106+
freshSetup = ~isfile(fullfile(Path.SettingsDir, 'LiquidCalibration.json')) && ~BpodLib.path.compatibility.isLegacySettings(Path.LocalDir);
107+
if freshSetup
107108
fileNames = {'LiquidCalibration.json', 'SoundCalibration.mat', 'Readme.txt'};
108109
for idx = 1:numel(fileNames)
109110
fileName = fileNames{idx};

Functions/+BpodLib/+calibration/+liquid/+compatibility/createDummyData.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@
5252
valve3.addMeasurement(35, 3.5);
5353
valve3.addMeasurement(66, 8.5);
5454

55-
% todo: these dates are different to the .JSON in Examples/
56-
% Update LastDateModified to reflect the dummy data
57-
valve1.LastDateModified = '2016-11-17T16:08:26'; % original from Bpod_Gen2
58-
% valve3.LastDateModified = char(datetime('2024-07-17 09:22:28')); % the date of this function's creation
59-
valve3.LastDateModified = "";
60-
55+
% Original MATLAB struct file had LastDateModified equivalent to 2016-11-17T16:08:26
56+
dateString = "2000-01-01T00:00:00";
57+
valve1.LastDateModified = dateString;
58+
valve3.LastDateModified = dateString;
6159
end

Functions/+BpodLib/+calibration/+liquid/+utils/checkCOM.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
return
4141
end
4242

43+
if strcmp(LiquidCal.metadata.COM, "example data")
44+
% If the file's COM is example data, the liquid cal is just placeholder
45+
% so we count that as "unknown"
46+
return
47+
end
48+
4349
if strcmp(LiquidCal.metadata.COM, BpodSystemCOM)
4450
matchingCOM = 'yes';
4551
else
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function createDefaultFile()
2+
% Create the default LiquidCalibration.json file in Examples/
3+
4+
LiquidCal= BpodLib.calibration.liquid.compatibility.createDummyData();
5+
rootPath = BpodLib.path.getPath('root');
6+
filePath = fullfile(rootPath, 'Examples/Example Calibration Files/LiquidCalibration.json');
7+
8+
% Insert data
9+
dateString = "2000-01-01T00:00:00";
10+
saveData = LiquidCal.createSaveData();
11+
saveData.metadata.modification_datetime = dateString;
12+
saveData.metadata.COM = "example data";
13+
14+
if verLessThan('matlab', '9.10') % R2021a is 9.10
15+
writeData = jsonencode(saveData);
16+
else
17+
writeData = jsonencode(saveData, 'PrettyPrint', true);
18+
end
19+
fid = fopen(filePath, 'w');
20+
fwrite(fid, writeData, 'char');
21+
fclose(fid);
22+
23+
24+
end

Tests/BpodLib/calibration/liquid/test_dataWarning.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ function test_loadFailure(testCase)
5555
% These should "fail" silently if the startup failed to load data
5656
BpodLib.calibration.liquid.io.checkDefaultData([]);
5757
BpodLib.calibration.liquid.io.report([])
58+
end
59+
60+
function test_load_fresh(testCase)
61+
BpodSystem = BpodTest.MockBpodObject('EMU');
62+
filepath = fullfile(BpodLib.path.getPath('root'), 'Examples/Example Calibration Files/LiquidCalibration.json');
63+
LiquidCal = BpodLib.calibration.liquid.ValveDataManagerClass('filepath', filepath);
64+
65+
BpodSystem.CalibrationTables.LiquidCal = LiquidCal;
66+
LiquidCal.metadata.COM
67+
BpodLib.calibration.liquid.utils.checkCOM(BpodSystem)
68+
testCase.verifyTrue(strcmp('unknown', BpodLib.calibration.liquid.utils.checkCOM(BpodSystem)));
5869
end

Tests/BpodLib/calibration/liquid/test_liquidcalibration.m

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function setupOnce(testCase)
2222
OriginalLiquidCal(3).Coeffs = [0.0479777954004750,5.72402854877083,13.6002180808882];
2323

2424
testCase.TestData.OriginalLiquidCal = OriginalLiquidCal;
25-
% testCase.TestData.expectedJSONPath = fullfile('testData/ExpectedLiquidCalibration.json');
2625
[testFolder, ~, ~] = fileparts(mfilename('fullpath'));
2726
testDataFolder = fullfile(testFolder, 'testData');
2827
testCase.TestData.expectedJSONPath = fullfile(testDataFolder, 'ExpectedLiquidCalibration.json');
@@ -34,33 +33,6 @@ function teardownOnce(testCase)
3433
rmdir(testCase.TestData.rootPath, 's');
3534
end
3635

37-
38-
function test_JSONWrite(testCase)
39-
% Test that the JSON format being saved by ValveManagerClass is correct
40-
41-
% Create test data and write JSON
42-
dummyValveManager = BpodLib.calibration.liquid.compatibility.createDummyData();
43-
dummyjsonPath = fullfile(testCase.TestData.rootPath, 'test.json');
44-
dummyValveManager.saveData(dummyjsonPath);
45-
46-
% Read both files
47-
jsonData = fileread(dummyjsonPath);
48-
expectedData = fileread(testCase.TestData.expectedJSONPath);
49-
50-
% Normalize both JSON strings
51-
jsonData = normalizeJSON(jsonData);
52-
expectedData = normalizeJSON(expectedData);
53-
54-
% Compare the normalized versions
55-
testCase.verifyEqual(jsonData, expectedData, 'JSON content does not match expected');
56-
57-
% If still failing, provide diagnostic output
58-
if ~isequal(jsonData, expectedData)
59-
fprintf('\n=== JSON COMPARISON FAILURE DETAILS ===\n');
60-
showDiff(jsonData, expectedData);
61-
end
62-
end
63-
6436
function test_JSONRead(testCase)
6537
% Test ValveDataManager's reading, ensuring values are in the right
6638
% places

0 commit comments

Comments
 (0)