Skip to content

Commit

Permalink
[FIX] remove byte order mark from tsv file (bids-standard#556)
Browse files Browse the repository at this point in the history
* fix bom

* add data

* octave fix
  • Loading branch information
Remi-Gau authored May 4, 2023
1 parent bda368a commit 236ade3
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
11 changes: 10 additions & 1 deletion +bids/+util/tsvread.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%
% file_content = tsvread(filename, field_to_return, hdr)
%
% :param filename: filename (can be gzipped) {txt,mat,csv,tsv,json}ename
% :param filename: filename (can be gzipped) {txt,mat,csv,tsv,json}
% :type filename: string
%
% :param field_to_return: name of field to return if data stored in a structure
Expand Down Expand Up @@ -198,6 +198,15 @@
if S(end) ~= eol
S = [S eol];
end
% Byte order mark (BOM),U+FEFF,0xEF,0xBB,0xBF
% matlab case
if S(1) == 65279
S(1) = [];
end
% octave case
if numel(S) > 2 && isequal(S(1:3), [239 187 191])
S(1:3) = [];
end
S = regexprep(S, {'\r\n', '\r', '(\n)\1+'}, {'\n', '\n', '$1'});

% -Get column names from header line (non-numeric first line)
Expand Down
91 changes: 91 additions & 0 deletions tests/data/bom_bug_552.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
onset duration trial_type value sample
61.824 5.0 Control 1 483
87.296 5.0 Control 1 682
117.632 5.0 Tapping/Right 3 919
146.816 5.0 Tapping/Right 3 1147
181.504 5.0 Control 1 1418
212.864 5.0 Tapping/Left 2 1663
240.64 5.0 Tapping/Left 2 1880
275.712 5.0 Control 1 2154
311.424 5.0 Tapping/Right 3 2433
344.832 5.0 Tapping/Right 3 2694
373.376 5.0 Tapping/Left 2 2917
404.736 5.0 Tapping/Left 2 3162
435.712 5.0 Tapping/Left 2 3404
474.496 5.0 Tapping/Right 3 3707
512.256 5.0 Tapping/Left 2 4002
544.64 5.0 Control 1 4255
570.368 5.0 Tapping/Left 2 4456
609.28 5.0 Tapping/Right 3 4760
635.904 5.0 Control 1 4968
661.504 5.0 Tapping/Left 2 5168
690.56 5.0 Tapping/Right 3 5395
720.256 5.0 Tapping/Left 2 5627
758.144 5.0 Control 1 5923
784.384 5.0 Control 1 6128
819.456 5.0 Tapping/Left 2 6402
847.872 5.0 Control 1 6624
874.368 5.0 Tapping/Left 2 6831
900.48 5.0 Control 1 7035
930.944 5.0 Tapping/Right 3 7273
965.76 5.0 Tapping/Left 2 7545
993.792 5.0 Tapping/Right 3 7764
1021.44 5.0 Control 1 7980
1046.784 5.0 Tapping/Right 3 8178
1080.192 5.0 Tapping/Left 2 8439
1106.56 5.0 Tapping/Right 3 8645
1144.448 5.0 Tapping/Right 3 8941
1182.72 5.0 Tapping/Left 2 9240
1220.48 5.0 Control 1 9535
1254.784 5.0 Tapping/Right 3 9803
1281.408 5.0 Tapping/Left 2 10011
1317.504 5.0 Tapping/Left 2 10293
1354.624 5.0 Tapping/Right 3 10583
1383.936 5.0 Control 1 10812
1416.064 5.0 Tapping/Right 3 11063
1456.0 5.0 Tapping/Right 3 11375
1485.824 5.0 Tapping/Left 2 11608
1513.344 5.0 Tapping/Right 3 11823
1539.2 5.0 Tapping/Left 2 12025
1565.44 5.0 Tapping/Right 3 12230
1604.736 5.0 Tapping/Left 2 12537
1638.016 5.0 Control 1 12797
1673.6 5.0 Tapping/Right 3 13075
1704.96 5.0 Control 1 13320
1734.784 5.0 Control 1 13553
1760.896 5.0 Tapping/Left 2 13757
1791.104 5.0 Tapping/Right 3 13993
1819.008 5.0 Tapping/Left 2 14211
1857.536 5.0 Control 1 14512
1896.832 5.0 Tapping/Left 2 14819
1936.256 5.0 Control 1 15127
1967.232 5.0 Tapping/Right 3 15369
2003.328 5.0 Tapping/Right 3 15651
2043.264 5.0 Tapping/Right 3 15963
2070.144 5.0 Tapping/Left 2 16173
2106.24 5.0 Tapping/Right 3 16455
2132.992 5.0 Control 1 16664
2166.784 5.0 Control 1 16928
2198.4 5.0 Tapping/Right 3 17175
2234.112 5.0 Tapping/Right 3 17454
2270.976 5.0 Control 1 17742
2305.536 5.0 Control 1 18012
2334.848 5.0 Tapping/Right 3 18241
2362.24 5.0 Tapping/Right 3 18455
2393.216 5.0 Control 1 18697
2432.64 5.0 Control 1 19005
2466.304 5.0 Control 1 19268
2505.216 5.0 Tapping/Left 2 19572
2542.72 5.0 Tapping/Left 2 19865
2578.432 5.0 Control 1 20144
2606.336 5.0 Tapping/Left 2 20362
2639.36 5.0 Tapping/Left 2 20620
2677.504 5.0 Tapping/Right 3 20918
2707.328 5.0 Control 1 21151
2735.616 5.0 Control 1 21372
2770.944 5.0 Tapping/Left 2 21648
2806.912 5.0 Control 1 21929
2833.408 5.0 Tapping/Left 2 22136
2869.376 5.0 Control 1 22417
2908.032 5.0 Tapping/Right 3 22719
2938.496 5.0 Tapping/Left 2 22957
8 changes: 8 additions & 0 deletions tests/tests_utils/test_tsvread.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ function test_tsvread_basic()

end

function test_tsvread_bug_552()

tsv_file = fullfile(get_test_data_dir(), '..', 'data', 'bom_bug_552.tsv');
content = bids.util.tsvread(tsv_file);
assert(ismember('onset', fieldnames(content)));

end

function [pth, expected] = fixture()

pth = fullfile(get_test_data_dir(), '..', 'data');
Expand Down

0 comments on commit 236ade3

Please sign in to comment.