Skip to content

Commit

Permalink
Merge pull request #30 from trossi/fix-compact_seq
Browse files Browse the repository at this point in the history
Fix bug in compact sequence constructor
  • Loading branch information
vnmabus committed Nov 21, 2023
2 parents 4dd2ea5 + a2dec57 commit d4c1d83
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
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

0 comments on commit d4c1d83

Please sign in to comment.