Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in compact sequence constructor #30

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions rdata/parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,21 @@ def compact_seq_constructor(
reference=0,
)

n = int(state.value[0])
start = state.value[1]
stop = state.value[0]
step = state.value[2]

if is_int:
start = int(start)
stop = int(stop)
step = int(step)

value = np.arange(start, stop, step)
# Calculate stop with integer arithmetic
# and use built-in range() for numerical stability
stop = start + (n - 1) * step
value = np.array(range(start, stop + 1, step))
else:
# Calculate stop with floating-point arithmetic
stop = start + (n - 1) * step
value = np.linspace(start, stop, n)

return new_info, value

Expand Down
Binary file not shown.
Binary file not shown.
32 changes: 32 additions & 0 deletions rdata/tests/test_rdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,22 @@ def test_altrep_compact_intseq(self) -> None:
"test_altrep_compact_intseq": np.arange(1000),
})

def test_altrep_compact_intseq_asymmetric(self) -> None:
"""
Test alternative representation of sequences of ints.

This test an origin different from 0, to reproduce
issue #29.
"""
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_compact_intseq_asymmetric.rda",
)
converted = rdata.conversion.convert(parsed)

np.testing.assert_equal(converted, {
"test_altrep_compact_intseq_asymmetric": np.arange(-5, 6),
})

def test_altrep_compact_realseq(self) -> None:
"""Test alternative representation of sequences of ints."""
parsed = rdata.parser.parse_file(
Expand All @@ -643,6 +659,22 @@ def test_altrep_compact_realseq(self) -> None:
"test_altrep_compact_realseq": np.arange(1000.0),
})

def test_altrep_compact_realseq_asymmetric(self) -> None:
"""
Test alternative representation of sequences of ints.

This test an origin different from 0, to reproduce
issue #29.
"""
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_compact_realseq_asymmetric.rda",
)
converted = rdata.conversion.convert(parsed)

np.testing.assert_equal(converted, {
"test_altrep_compact_realseq_asymmetric": np.arange(-5.0, 6.0),
})

def test_altrep_deferred_string(self) -> None:
"""Test alternative representation of deferred strings."""
parsed = rdata.parser.parse_file(
Expand Down