mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-09 11:19:23 +08:00
MAINT: Add a reader which dispatches on asset type
Add `AssetDispatchSessionBarReader` and corresponding minute and session bar version of that reader. This reader routes requests to the appropriate reader based on the asset type of the requested sids. `load_raw_array` in the dispatch reader batches the sid by asset type and then interleaves the results in the out arrays, so that the arrays data corresponds with sids in the order that sids are passed to the method, to meet the expected behavior of `load_raw_arrays`. The dispatch redaer is intended for use by the data portal when using both future and equities. The dispatch reader will also be passed to the to the `HistoryLoader`s contained within the data portal, where the batched `load_raw_arrays` will be used. Also, BUG: - Fix the return of `MinuteResampleSessionBarReader.load_raw_arrays` to match all other readers. - Use the input dt for the `MinuteResampleSessionBarReader.load_raw_arrays` as a session label, instead of a minute dt, since it is a session bar reader. (Both of these bugs where discovered when using the resample reader for future data in the dispatch tests.)
This commit is contained in:
@@ -468,16 +468,37 @@ class MinuteResampleSessionBarReader(SessionBarReader):
|
||||
minute_data = self._minute_bar_reader.load_raw_arrays(
|
||||
columns, start_dt, end_dt, assets)
|
||||
dts = self._calendar.minutes_in_range(start_dt, end_dt)
|
||||
minute_frame = DataFrame(
|
||||
[d.T[0] for d in minute_data], index=columns, columns=dts).T
|
||||
return minute_to_session(minute_frame, self._calendar)
|
||||
frames = []
|
||||
for i, _ in enumerate(assets):
|
||||
minute_frame = DataFrame((d.T[i] for d in minute_data),
|
||||
index=columns, columns=dts).T
|
||||
df = minute_to_session(minute_frame, self._calendar)
|
||||
frames.append(df)
|
||||
return frames
|
||||
|
||||
@property
|
||||
def trading_calendar(self):
|
||||
return self._calendar
|
||||
|
||||
def load_raw_arrays(self, columns, start_dt, end_dt, assets):
|
||||
return self._get_resampled(columns, start_dt, end_dt, assets).values
|
||||
def load_raw_arrays(self, columns, start_dt, end_dt, sids):
|
||||
sessions = self._calendar.sessions_in_range(start_dt, end_dt)
|
||||
range_open, _ = self._calendar.open_and_close_for_session(
|
||||
start_dt)
|
||||
_, range_close = self._calendar.open_and_close_for_session(
|
||||
end_dt)
|
||||
shape = len(sessions), len(sids)
|
||||
results = []
|
||||
for col in columns:
|
||||
if col != 'volume':
|
||||
out = np.full(shape, np.nan)
|
||||
else:
|
||||
out = np.zeros(shape, dtype=np.uint32)
|
||||
results.append(out)
|
||||
frames = self._get_resampled(columns, range_open, range_close, sids)
|
||||
for i, result in enumerate(results):
|
||||
for j, frame in enumerate(frames):
|
||||
result[:, j] = frame.values[:, i]
|
||||
return results
|
||||
|
||||
def get_value(self, sid, session, colname):
|
||||
# WARNING: This will need caching or other optimization if used in a
|
||||
@@ -485,7 +506,7 @@ class MinuteResampleSessionBarReader(SessionBarReader):
|
||||
# This was developed to complete interface, but has not been tuned
|
||||
# for real world use.
|
||||
start, end = self._calendar.open_and_close_for_session(session)
|
||||
frame = self._get_resampled([colname], start, end, [sid])
|
||||
frame = self._get_resampled([colname], start, end, [sid])[0]
|
||||
return frame.loc[session, colname]
|
||||
|
||||
@lazyval
|
||||
@@ -590,8 +611,11 @@ class ReindexBarReader(with_metaclass(ABCMeta)):
|
||||
|
||||
outer_results = []
|
||||
|
||||
inner_results = self._reader.load_raw_arrays(
|
||||
fields, inner_dts[0], inner_dts[-1], sids)
|
||||
if len(inner_dts) > 0:
|
||||
inner_results = self._reader.load_raw_arrays(
|
||||
fields, inner_dts[0], inner_dts[-1], sids)
|
||||
else:
|
||||
inner_results = None
|
||||
|
||||
for i, field in enumerate(fields):
|
||||
if field != 'volume':
|
||||
@@ -599,7 +623,8 @@ class ReindexBarReader(with_metaclass(ABCMeta)):
|
||||
else:
|
||||
out = np.zeros(shape, dtype=np.uint32)
|
||||
|
||||
out[indices] = inner_results[i]
|
||||
if inner_results is not None:
|
||||
out[indices] = inner_results[i]
|
||||
|
||||
outer_results.append(out)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user