Repository: quantopian/zipline
File: zipline/data/history_loader.py (Python)

Code before:
from abc import (
    ABCMeta,
    abstractmethod,
    abstractproperty,
)
from lru import LRU

from numpy import around, hstack
from pandas.tslib import normalize_date

from six import with_metaclass

from zipline.lib._float64window import AdjustedArrayWindow as Float64Window
from zipline.lib.adjustment import Float64Multiply
from zipline.utils.cache import ExpiringCache
from zipline.utils.memoize import lazyval
from zipline.utils.numpy_utils import float64_dtype


class HistoryCompatibleUSEquityAdjustmentReader(object):

    def __init__(self, adjustment_reader):
        self._adjustments_reader = adjustment_reader

    def load_adjustments(self, columns, dts, assets, output_offset=0):
        """
        Returns
        -------
        adjustments : list[dict[int -> Adjustment]]
            A list, where each element corresponds to the `columns`, of
            mappings from index to adjustment objects to apply at that index.
        """
        out = [None] * len(columns)
        for i, column in enumerate(columns):
            adjs = {}
            for asset in assets:
                adjs.update(self._get_adjustments_in_range(
                    asset, dts, column, output_offset))
            out[i] = adjs
        return out

    def _get_adjustments_in_range(self, asset, dts, field,
                                  output_offset=0):
        """
        Get the Float64Multiply objects to pass to an AdjustedArrayWindow.

        For the use of AdjustedArrayWindow in the loader, which looks back
        from current simulation time back to a window of data the dictionary is
        structured with:
        - the key into the dictionary for adjustments is the location of the
        day from which the window is being viewed.

Review comment (suggestion):
@ssanderson do you have a better alternative than `output_offset` for this value which changes the values so that `AdjustedArrayWindow` pops the adjustment an index earlier? e.g. `{2: Float64Window(0, 2, 0, 0, 0.5)`} becomes `{1: Float64Window(0, 2, 0, 0, 0.5)`


Code after:
from abc import (
    ABCMeta,
    abstractmethod,
    abstractproperty,
)
from lru import LRU

from numpy import around, hstack
from pandas.tslib import normalize_date

from six import with_metaclass

from zipline.lib._int64window import AdjustedArrayWindow as Int64Window
from zipline.lib._float64window import AdjustedArrayWindow as Float64Window
from zipline.lib.adjustment import Float64Multiply
from zipline.utils.cache import ExpiringCache
from zipline.utils.memoize import lazyval
from zipline.utils.numpy_utils import float64_dtype


class HistoryCompatibleUSEquityAdjustmentReader(object):

    def __init__(self, adjustment_reader):
        self._adjustments_reader = adjustment_reader

    def load_adjustments(self, columns, dts, assets, output_offset=0):
        """
        Returns
        -------
        adjustments : list[dict[int -> Adjustment]]
            A list, where each element corresponds to the `columns`, of
            mappings from index to adjustment objects to apply at that index.
        """
        out = [None] * len(columns)
        for i, column in enumerate(columns):
            adjs = {}
            for asset in assets:
                adjs.update(self._get_adjustments_in_range(
                    asset, dts, column))
            out[i] = adjs
        return out

    def _get_adjustments_in_range(self, asset, dts, field):
        """
        Get the Float64Multiply objects to pass to an AdjustedArrayWindow.

        For the use of AdjustedArrayWindow in the loader, which looks back
        from current simulation time back to a window of data the dictionary is
        structured with:
        - the key into the dictionary for adjustments is the location of the
        day from which the window is being viewed.