Skip to content

Commit 76bb3e9

Browse files
committed
FIX: TSV2JSON should convert empty TSV files to empty JSON files
1 parent 76763b1 commit 76bb3e9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

niworkflows/interfaces/tests/test_utility.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
# https://www.nipreps.org/community/licensing/
2222
#
2323
"""KeySelect tests."""
24+
from pathlib import Path
2425
import pytest
25-
from ..utility import KeySelect
26+
from ..utility import KeySelect, _tsv2json
2627

2728

2829
def test_KeySelect():
@@ -32,3 +33,11 @@ def test_KeySelect():
3233

3334
with pytest.raises(ValueError):
3435
KeySelect(fields=[])
36+
37+
38+
def test_tsv2json(tmp_path):
39+
Path.write_bytes(tmp_path / 'empty.tsv', bytes())
40+
res = _tsv2json(tmp_path / 'empty.tsv', None, 'any_column')
41+
assert res == {}
42+
res = _tsv2json(tmp_path / 'empty.tsv', None, 'any_column', additional_metadata={'a': 'b'})
43+
assert res == {}

niworkflows/interfaces/utility.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,16 @@ def less_breakable(a_string):
545545

546546
drop_columns = drop_columns or []
547547
additional_metadata = additional_metadata or {}
548-
tsv_data = pd.read_csv(in_tsv, "\t")
548+
try:
549+
tsv_data = pd.read_csv(in_tsv, "\t")
550+
except pd.errors.EmptyDataError:
551+
tsv_data = pd.DataFrame()
549552
for k, v in additional_metadata.items():
550553
tsv_data[k] = [v] * len(tsv_data.index)
551554
for col in drop_columns:
552555
tsv_data.drop(labels=col, axis="columns", inplace=True)
553-
tsv_data.set_index(index_column, drop=True, inplace=True)
556+
if index_column in tsv_data:
557+
tsv_data.set_index(index_column, drop=True, inplace=True)
554558
if enforce_case:
555559
tsv_data.index = [
556560
re.sub(re_to_snake, snake, less_breakable(i), 0).lower()

0 commit comments

Comments
 (0)