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

Code before:
        start_date: Timestamp
           Beginning of the window range.
        end_date: Timestamp
           End of the window range.
        assets : list of int
           The asset identifiers in the window.

        Returns
        -------
        list of np.ndarray
            A list with an entry per field of ndarrays with shape
            (minutes in range, sids) with a dtype of float64, containing the
            values for the respective field over start and end dt range.
        """
        self._validate_timestamp(start_date)
        self._validate_timestamp(end_date)

        start = start_date.asm8
        end = end_date.asm8

        sid_selector, out_buf_indexer = self._make_sid_indexers(assets)

        date_slice = self._compute_date_range_slice(start, end)

        n_dates = date_slice.stop - date_slice.start
        n_query_sids = len(assets)
        n_valid_sids = len(self.sids)

        read_buf = np.zeros((n_valid_sids, n_dates), dtype=np.uint32)
        out_buf = np.zeros((n_query_sids, n_dates), dtype=np.uint32)

        out = []
        for column in columns:
            # Zero the buffers to prepare to receive new data.
            read_buf.fill(0)
            out_buf.fill(0)

            dataset = self._country_group[DATA][column]

            dataset.read_direct(
                read_buf,
                np.s_[:, date_slice.start:date_slice.stop],
            )

            out_buf[out_buf_indexer] = read_buf[sid_selector]

            out.append(
                self._postprocessors[column](
                    out_buf.T.copy()
                )
            )

Review comment (suggestion):
The fact that the order of these two variables is transposed from the order in which they're used to initialize `read_buf`/`out_buf` below tripped me up a bit when trying to keep these variables straight.

Code after:
        start_date: Timestamp
           Beginning of the window range.
        end_date: Timestamp
           End of the window range.
        assets : list of int
           The asset identifiers in the window.

        Returns
        -------
        list of np.ndarray
            A list with an entry per field of ndarrays with shape
            (minutes in range, sids) with a dtype of float64, containing the
            values for the respective field over start and end dt range.
        """
        self._validate_timestamp(start_date)
        self._validate_timestamp(end_date)

        start = start_date.asm8
        end = end_date.asm8
        date_slice = self._compute_date_range_slice(start, end)
        n_dates = date_slice.stop - date_slice.start

        # Create a buffer into which we'll read data from the h5 file.
        # Allocate an extra row of space that will always contain null values.
        # We'll use that space to provide "data" for entries in ``assets`` that
        # are unknown to us.
        full_buf = np.zeros((len(self.sids) + 1, n_dates), dtype=np.uint32)
        # We'll only read values into this portion of the read buf.
        mutable_buf = full_buf[:-1]

        # Indexer that converts an array aligned to self.sids (which is what we
        # pull from the h5 file) into an array aligned to ``assets``.
        #
        # Unknown assets will have an index of -1, which means they'll always
        # pull from the last row of the read buffer. We allocated an extra
        # empty row above so that these lookups will cause us to fill our
        # output buffer with "null" values.
        sid_selector = self._make_sid_selector(assets)

        out = []
        for column in columns:
            # Zero the buffer to prepare to receive new data.
            mutable_buf.fill(0)

            dataset = self._country_group[DATA][column]

            # Fill the mutable portion of our buffer with data from the file.
            dataset.read_direct(
                mutable_buf,
                np.s_[:, date_slice],
            )