Skip to content

Commit 4b88258

Browse files
committed
Fix importing json templates
1 parent e5eec2d commit 4b88258

File tree

10 files changed

+72
-70
lines changed

10 files changed

+72
-70
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-include vectorbt/templates *.json

setup.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66
setup(
77
name='vectorbt',
8-
version='0.15.1',
8+
version='0.15.2',
99
description='Python library for backtesting and analyzing trading strategies at scale',
1010
author='Oleg Polakow',
1111
author_email='[email protected]',
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
url='https://github.com/polakowo/vectorbt',
1515
packages=find_packages(),
16+
package_data={
17+
'vectorbt': ['templates/*.json']
18+
},
1619
install_requires=[
1720
'numpy',
1821
'pandas',

tests/test_generic.py

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from vectorbt.generic import nb
99
from vectorbt.generic.drawdowns import Drawdowns
1010

11+
seed = 42
12+
1113
day_dt = np.timedelta64(86400000000000)
1214

1315
df = pd.DataFrame({
@@ -186,6 +188,34 @@ def test_split_into_ranges(self):
186188
with pytest.raises(Exception) as e_info:
187189
df.vbt.split_into_ranges(start_idxs=[0, 1], end_idxs=[2, 4])
188190

191+
def test_shuffle(self):
192+
pd.testing.assert_series_equal(
193+
df['a'].vbt.shuffle(seed=seed),
194+
pd.Series(
195+
np.array([2.0, np.nan, 3.0, 1.0, 4.0]),
196+
index=df['a'].index,
197+
name=df['a'].name
198+
)
199+
)
200+
np.testing.assert_array_equal(
201+
df['a'].vbt.shuffle(seed=seed).values,
202+
nb.shuffle_1d_nb(df['a'].values, seed=seed)
203+
)
204+
pd.testing.assert_frame_equal(
205+
df.vbt.shuffle(seed=seed),
206+
pd.DataFrame(
207+
np.array([
208+
[2., 2., 2.],
209+
[np.nan, 4., 1.],
210+
[3., 3., 2.],
211+
[1., np.nan, 1.],
212+
[4., 1., np.nan]
213+
]),
214+
index=df.index,
215+
columns=df.columns
216+
)
217+
)
218+
189219
@pytest.mark.parametrize(
190220
"test_value",
191221
[-1, 0., np.nan],

tests/test_signals.py

-28
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,6 @@ def test_freq(self):
5050
assert pd.Series([False, True]).vbt.signals(freq='3D').wrapper.freq == day_dt * 3
5151
assert pd.Series([False, True]).vbt.signals(freq=np.timedelta64(4, 'D')).wrapper.freq == day_dt * 4
5252

53-
def test_shuffle(self):
54-
pd.testing.assert_series_equal(
55-
sig['a'].vbt.signals.shuffle(seed=seed),
56-
pd.Series(
57-
np.array([False, False, False, True, True]),
58-
index=sig['a'].index,
59-
name=sig['a'].name
60-
)
61-
)
62-
np.testing.assert_array_equal(
63-
sig['a'].vbt.signals.shuffle(seed=seed).values,
64-
nb.shuffle_1d_nb(sig['a'].values, seed=seed)
65-
)
66-
pd.testing.assert_frame_equal(
67-
sig.vbt.signals.shuffle(seed=seed),
68-
pd.DataFrame(
69-
np.array([
70-
[False, False, False],
71-
[False, True, False],
72-
[False, False, False],
73-
[True, False, False],
74-
[True, True, True]
75-
]),
76-
index=sig.index,
77-
columns=sig.columns
78-
)
79-
)
80-
8153
@pytest.mark.parametrize(
8254
"test_n",
8355
[1, 2, 3, 4, 5],

vectorbt/base/accessors.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def empty(cls, shape, fill_value=np.nan, **kwargs):
6565
return pd.DataFrame(np.full(shape, fill_value), **kwargs)
6666

6767
@classmethod
68-
def empty_like(cls, other, fill_value=np.nan):
68+
def empty_like(cls, other, fill_value=np.nan, **kwargs):
6969
"""Generate an empty Series/DataFrame like `other` and fill with `fill_value`."""
7070
if checks.is_series(other):
71-
return cls.empty(other.shape, fill_value=fill_value, index=other.index, name=other.name)
72-
return cls.empty(other.shape, fill_value=fill_value, index=other.index, columns=other.columns)
71+
return cls.empty(other.shape, fill_value=fill_value, index=other.index, name=other.name, **kwargs)
72+
return cls.empty(other.shape, fill_value=fill_value, index=other.index, columns=other.columns, **kwargs)
7373

7474
# ############# Index and columns ############# #
7575

@@ -336,10 +336,10 @@ def concat(self_or_cls, *others, keys=None, broadcast_kwargs={}):
336336
objs = (self_or_cls._obj,) + others
337337
broadcasted = reshape_fns.broadcast(*objs, **broadcast_kwargs)
338338
broadcasted = tuple(map(reshape_fns.to_2d, broadcasted))
339-
concatenated = pd.concat(broadcasted, axis=1, keys=keys)
340-
if np.all(concatenated.columns == 0):
341-
concatenated.columns = pd.RangeIndex(start=0, stop=len(concatenated.columns), step=1)
342-
return concatenated
339+
out = pd.concat(broadcasted, axis=1, keys=keys)
340+
if not isinstance(out.columns, pd.MultiIndex) and np.all(out.columns == 0):
341+
out.columns = pd.RangeIndex(start=0, stop=len(out.columns), step=1)
342+
return out
343343

344344
def apply_and_concat(self, ntimes, *args, apply_func=None, to_2d=False, keys=None, **kwargs):
345345
"""Apply `apply_func` `ntimes` times and concatenate the results along columns.

vectorbt/generic/accessors.py

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575

7676
@add_nb_methods([
77+
nb.shuffle_nb,
7778
nb.fillna_nb,
7879
nb.fshift_nb,
7980
nb.diff_nb,

vectorbt/generic/nb.py

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@
1717
from vectorbt.generic.enums import DrawdownStatus, drawdown_dt
1818

1919

20+
@njit(cache=True)
21+
def shuffle_1d_nb(a, seed=None):
22+
"""Shuffle each column in `a`.
23+
24+
Specify `seed` to make output deterministic."""
25+
if seed is not None:
26+
np.random.seed(seed)
27+
return np.random.permutation(a)
28+
29+
30+
@njit(cache=True)
31+
def shuffle_nb(a, seed=None):
32+
"""2-dim version of `shuffle_1d_nb`."""
33+
if seed is not None:
34+
np.random.seed(seed)
35+
out = np.empty_like(a, dtype=a.dtype)
36+
37+
for col in range(a.shape[1]):
38+
out[:, col] = np.random.permutation(a[:, col])
39+
return out
40+
41+
2042
@njit(cache=True)
2143
def prepend_1d_nb(a, n, value):
2244
"""Prepend value `n` times."""

vectorbt/settings.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import numpy as np
1414
import json
15-
import os
15+
import pkgutil
1616

1717
from vectorbt.utils.config import Config
1818

@@ -60,17 +60,13 @@
6060
"""
6161

6262
# Templates
63-
with open(os.path.join(os.path.dirname(__file__), 'templates/light.json')) as json_file:
64-
light_template = Config(json.load(json_file))
65-
"""_"""
63+
light_template = Config(json.loads(pkgutil.get_data(__name__, "templates/light.json")))
6664

67-
__pdoc__['light_template'] = "Light template."
65+
__pdoc__['light_template'] = "Light template."
6866

69-
with open(os.path.join(os.path.dirname(__file__), 'templates/dark.json')) as json_file:
70-
dark_template = Config(json.load(json_file))
71-
"""_"""
67+
dark_template = Config(json.loads(pkgutil.get_data(__name__, "templates/dark.json")))
7268

73-
__pdoc__['dark_template'] = "Dark template."
69+
__pdoc__['dark_template'] = "Dark template."
7470

7571
# Layout
7672
layout = Config(

vectorbt/signals/accessors.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050

5151

5252
@add_nb_methods([
53-
nb.shuffle_nb,
5453
nb.fshift_nb,
5554
], module_name='vectorbt.signals.nb')
5655
class Signals_Accessor(Generic_Accessor):
@@ -90,7 +89,7 @@ def empty(cls, *args, fill_value=False, **kwargs):
9089
2020-01-05 False False False
9190
```
9291
"""
93-
return Generic_Accessor.empty(*args, fill_value=fill_value, **kwargs)
92+
return Generic_Accessor.empty(*args, fill_value=fill_value, dtype=bool, **kwargs)
9493

9594
@classmethod
9695
def empty_like(cls, *args, fill_value=False, **kwargs):
@@ -116,7 +115,7 @@ def empty_like(cls, *args, fill_value=False, **kwargs):
116115
2020-01-05 False False False
117116
```
118117
"""
119-
return Generic_Accessor.empty_like(*args, fill_value=fill_value, **kwargs)
118+
return Generic_Accessor.empty_like(*args, fill_value=fill_value, dtype=bool, **kwargs)
120119

121120
# ############# Signal generation ############# #
122121

vectorbt/signals/nb.py

-22
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,6 @@ def generate_enex_nb(shape, entry_wait, exit_wait, entry_choice_func_nb,
145145
# ############# Random ############# #
146146

147147

148-
@njit(cache=True)
149-
def shuffle_1d_nb(a, seed=None):
150-
"""Shuffle each column in `a`.
151-
152-
Specify `seed` to make output deterministic."""
153-
if seed is not None:
154-
np.random.seed(seed)
155-
return np.random.permutation(a)
156-
157-
158-
@njit(cache=True)
159-
def shuffle_nb(a, seed=None):
160-
"""2-dim version of `shuffle_1d_nb`."""
161-
if seed is not None:
162-
np.random.seed(seed)
163-
out = np.empty_like(a, dtype=np.bool_)
164-
165-
for col in range(a.shape[1]):
166-
out[:, col] = np.random.permutation(a[:, col])
167-
return out
168-
169-
170148
@njit(cache=True)
171149
def rand_choice_nb(from_i, to_i, col, n):
172150
"""`choice_func_nb` to randomly pick `n` values from range `[from_i, to_i)`.

0 commit comments

Comments
 (0)