Skip to content

Commit f122724

Browse files
committed
parsing time series
1 parent 2648d66 commit f122724

File tree

5 files changed

+160
-3
lines changed

5 files changed

+160
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ lint: ## check style with flake8 and isort
6262
invoke lint
6363

6464
.PHONY: lint-sigllm
65-
lint: ## check style with flake8 and isort
65+
lint-sigllm: ## check style with flake8 and isort
6666
flake8 sigllm tests
6767
isort -c --recursive sigllm tests
6868

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
history = history_file.read()
1313

1414
install_requires = [
15+
'numpy',
1516
]
1617

1718
setup_requires = [

sigllm/data.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Data preprocessing module.
5+
6+
This module contains functions to help parse time series into
7+
text, preparing it for a language model.
8+
"""
9+
10+
import numpy as np
11+
12+
13+
def sig2str(values, sep=',', space=False, decimal=0):
14+
"""Convert a signal to a string.
15+
16+
Convert a 1-dimensional time series into text by casting it
17+
to integer values then into a string.
18+
19+
Args:
20+
values (numpy.ndarray):
21+
A sequence of signal values.
22+
sep (str):
23+
String to separate each element in values, Default to `","`.
24+
space (bool):
25+
Whether to add space between each digit in the result. Default to `False`.
26+
decimal (int):
27+
Number of decimal points to keep from the float representation. Default to `0`.
28+
29+
Returns:
30+
str:
31+
Text containing the elements of `values`.
32+
"""
33+
sign = 1 * (values >= 0) - 1 * (values < 0)
34+
values = np.abs(values)
35+
36+
sequence = sign * (values * 10**decimal).astype(int)
37+
38+
res = sep.join([str(num) for num in sequence])
39+
if space:
40+
res = ' '.join(res)
41+
42+
return res
43+
44+
45+
def str2sig(text, sep=',', decimal=0):
46+
"""Convert a text string to a signal.
47+
48+
Convert a string containing digits into an array of numbers.
49+
50+
Args:
51+
text (str):
52+
A string containing signal values.
53+
sep (str):
54+
String that was used to separate each element in text, Default to `","`.
55+
decimal (int):
56+
Number of decimal points to shift each element in text to. Default to `0`.
57+
58+
Returns:
59+
numpy.ndarray:
60+
A 1-dimensional array containing parsed elements in `text`.
61+
"""
62+
values = np.fromstring(text, dtype=float, sep=sep)
63+
return values * 10**(-decimal)

tests/test_data.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
from pytest import fixture
5+
6+
from sigllm.data import sig2str, str2sig
7+
8+
9+
@fixture
10+
def integers():
11+
return np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
12+
13+
14+
@fixture
15+
def floats():
16+
return np.array([
17+
1.283,
18+
2.424,
19+
3.213,
20+
4.583,
21+
5.486,
22+
6.284,
23+
7.297,
24+
8.023,
25+
9.786
26+
])
27+
28+
29+
@fixture
30+
def text():
31+
return '1,2,3,4,5,6,7,8,9'
32+
33+
34+
def test_sig2str(integers):
35+
expected = '1,2,3,4,5,6,7,8,9'
36+
37+
result = sig2str(integers)
38+
39+
assert result == expected
40+
41+
42+
def test_sig2str_decimal(integers):
43+
expected = '100,200,300,400,500,600,700,800,900'
44+
45+
result = sig2str(integers, decimal=2)
46+
47+
assert result == expected
48+
49+
50+
def test_sig2str_sep(integers):
51+
expected = '1|2|3|4|5|6|7|8|9'
52+
53+
result = sig2str(integers, sep='|')
54+
55+
assert result == expected
56+
57+
58+
def test_sig2str_space(integers):
59+
expected = '1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9'
60+
61+
result = sig2str(integers, space=True)
62+
63+
assert result == expected
64+
65+
66+
def test_sig2str_float(floats):
67+
expected = '1,2,3,4,5,6,7,8,9'
68+
69+
result = sig2str(floats)
70+
71+
assert result == expected
72+
73+
74+
def test_sig2str_float_decimal(floats):
75+
expected = '128,242,321,458,548,628,729,802,978'
76+
77+
result = sig2str(floats, decimal=2)
78+
79+
assert result == expected
80+
81+
82+
def test_str2sig(text):
83+
expected = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
84+
85+
result = str2sig(text)
86+
87+
np.testing.assert_equal(result, expected)
88+
89+
90+
def test_str2sig_decimal(text):
91+
expected = np.array([.01, .02, .03, .04, .05, .06, .07, .08, .09])
92+
93+
result = str2sig(text, decimal=2)
94+
95+
np.testing.assert_equal(result, expected)

tests/test_sigllm.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ class TestSigllm(unittest.TestCase):
1313

1414
def setUp(self):
1515
"""Set up test fixtures, if any."""
16-
pass
1716

1817
def tearDown(self):
1918
"""Tear down test fixtures, if any."""
20-
pass
2119

2220
def test_000_something(self):
2321
"""Test something."""

0 commit comments

Comments
 (0)