mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-07 19:07:50 +08:00
5f190395ad
- Adds a new class, ``LabelArray``, which is a subclass of np.ndarray. LabelArray is conceptually similar to pandas.Categorical, in that it stores data with many duplicate values as indices into an array of unique values. For string data with many duplicates (e.g. time-series of tickers or or industry classifications), this provides multiple orders of magnitude of improvement when doing string operations, especially string comparison/matching operations. - Adds a new generic object "specialization" for `AdjustedArrayWindow`, and a corresponding ObjectOverwrite adjustment. - Adds a new ``postprocess`` method to ``zipline.pipeline.term.Term``. This method is called on the final result of any pipeline expression after screen filtering has occurred. The default implementation of ``postprocess`` is identity, but Classifier overrides it to coerce string columns into pandas.Categoricals before presenting them to the user.
50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
"""
|
|
Utilities for creating public APIs (e.g. argument validation decorators).
|
|
"""
|
|
from zipline.utils.input_validation import preprocess
|
|
|
|
|
|
def restrict_to_dtype(dtype, message_template):
|
|
"""
|
|
A factory for decorators that restrict Term methods to only be callable on
|
|
Terms with a specific dtype.
|
|
|
|
This is conceptually similar to
|
|
zipline.utils.input_validation.expect_dtypes, but provides more flexibility
|
|
for providing error messages that are specifically targeting Term methods.
|
|
|
|
Parameters
|
|
----------
|
|
dtype : numpy.dtype
|
|
The dtype on which the decorated method may be called.
|
|
message_template : str
|
|
A template for the error message to be raised.
|
|
`message_template.format` will be called with keyword arguments
|
|
`method_name`, `expected_dtype`, and `received_dtype`.
|
|
|
|
Usage
|
|
-----
|
|
@restrict_to_dtype(
|
|
dtype=float64_dtype,
|
|
message_template=(
|
|
"{method_name}() was called on a factor of dtype {received_dtype}."
|
|
"{method_name}() requires factors of dtype{expected_dtype}."
|
|
|
|
),
|
|
)
|
|
def some_factor_method(self, ...):
|
|
self.stuff_that_requires_being_float64(...)
|
|
"""
|
|
def processor(term_method, _, term_instance):
|
|
term_dtype = term_instance.dtype
|
|
if term_dtype != dtype:
|
|
raise TypeError(
|
|
message_template.format(
|
|
method_name=term_method.__name__,
|
|
expected_dtype=dtype.name,
|
|
received_dtype=term_dtype,
|
|
)
|
|
)
|
|
return term_instance
|
|
return preprocess(self=processor)
|