Skip to content

Commit

Permalink
Merge pull request #746 from effigies/fix/empty-tsv2json
Browse files Browse the repository at this point in the history
FIX: TSV2JSON should convert empty TSV files to empty JSON files
  • Loading branch information
effigies authored Sep 13, 2022
2 parents 76763b1 + 76bb3e9 commit 5a88dda
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 10 additions & 1 deletion niworkflows/interfaces/tests/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
# https://www.nipreps.org/community/licensing/
#
"""KeySelect tests."""
from pathlib import Path
import pytest
from ..utility import KeySelect
from ..utility import KeySelect, _tsv2json


def test_KeySelect():
Expand All @@ -32,3 +33,11 @@ def test_KeySelect():

with pytest.raises(ValueError):
KeySelect(fields=[])


def test_tsv2json(tmp_path):
Path.write_bytes(tmp_path / 'empty.tsv', bytes())
res = _tsv2json(tmp_path / 'empty.tsv', None, 'any_column')
assert res == {}
res = _tsv2json(tmp_path / 'empty.tsv', None, 'any_column', additional_metadata={'a': 'b'})
assert res == {}
8 changes: 6 additions & 2 deletions niworkflows/interfaces/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,16 @@ def less_breakable(a_string):

drop_columns = drop_columns or []
additional_metadata = additional_metadata or {}
tsv_data = pd.read_csv(in_tsv, "\t")
try:
tsv_data = pd.read_csv(in_tsv, "\t")
except pd.errors.EmptyDataError:
tsv_data = pd.DataFrame()
for k, v in additional_metadata.items():
tsv_data[k] = [v] * len(tsv_data.index)
for col in drop_columns:
tsv_data.drop(labels=col, axis="columns", inplace=True)
tsv_data.set_index(index_column, drop=True, inplace=True)
if index_column in tsv_data:
tsv_data.set_index(index_column, drop=True, inplace=True)
if enforce_case:
tsv_data.index = [
re.sub(re_to_snake, snake, less_breakable(i), 0).lower()
Expand Down

0 comments on commit 5a88dda

Please sign in to comment.