mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-11 23:40:33 +08:00
Fixes crashes when using numpy log on a batch transform data panel.
The recent change to the creation of the data panel ended up with a panel with the dtype of 'object', which was causing numpy ufuncs like `log` to crash out on an `AttributeError`. This forces all frames in the panel to use a dtype of 'float', we may want to look at seeting a dtype on a frame by frame basis, e.g. 'volume' may more accurately be 'int'.
This commit is contained in:
@@ -267,6 +267,12 @@ def return_data(data, *args, **kwargs):
|
||||
return data
|
||||
|
||||
|
||||
@batch_transform
|
||||
def uses_ufunc(data, *args, **kwargs):
|
||||
# ufuncs like np.log should not crash
|
||||
return np.log(data)
|
||||
|
||||
|
||||
class BatchTransformAlgorithm(TradingAlgorithm):
|
||||
def initialize(self, *args, **kwargs):
|
||||
self.refresh_period = kwargs.pop('refresh_period', 1)
|
||||
@@ -348,6 +354,12 @@ class BatchTransformAlgorithm(TradingAlgorithm):
|
||||
compute_only_full=False
|
||||
)
|
||||
|
||||
self.uses_ufunc = uses_ufunc(
|
||||
refresh_period=self.refresh_period,
|
||||
window_length=self.window_length,
|
||||
clean_nans=False
|
||||
)
|
||||
|
||||
self.iter = 0
|
||||
|
||||
self.set_slippage(FixedSlippage())
|
||||
@@ -364,6 +376,7 @@ class BatchTransformAlgorithm(TradingAlgorithm):
|
||||
self.return_ticks.handle_data(data))
|
||||
self.history_return_not_full.append(
|
||||
self.return_not_full.handle_data(data))
|
||||
self.uses_ufunc.handle_data(data)
|
||||
|
||||
new_data = deepcopy(data)
|
||||
for sid in new_data:
|
||||
|
||||
@@ -472,7 +472,8 @@ class BatchTransform(EventWindow):
|
||||
# user-overloaded get_value() method.
|
||||
data_dict = {tick['dt']: tick['data'] for tick in self.ticks}
|
||||
data = pd.Panel(data_dict, major_axis=self.field_names,
|
||||
minor_axis=self.sids)
|
||||
minor_axis=self.sids,
|
||||
dtype='float')
|
||||
|
||||
if self.supplemental_data:
|
||||
# item will be a date stamp
|
||||
|
||||
Reference in New Issue
Block a user