Exemplo n.º 1
0
    def extract_rows(self, *clipID_or_IDs):
        """
        Find and return the v-stacked distance vectors, in kernel row order
        (i.e. not in the order given as arguments), of the kernel rows matching
        the given clip IDs.

        Note: The matrix returned will always be a new instance and not set up
        to use shared memory. When directly used with shared memory objects, it
        will be passed by value, not by reference.

        :param clipID_or_IDs: The integer clip ID or IDs of which to get the
            distance vectors for.
        :type clipID_or_IDs: int or Iterable of int

        :return: The row-wise index-to-clipID map (tuple), the column-wise
            index-to-clipID map (tuple), and the KxL shape matrix, where K is
            the number of clip IDs given to the method, and L is the width
            (columns) of the distance kernel.
        :rtype: tuple of int, tuple of int, matrix

        """
        with self._rw_lock.read_lock():
            with SimpleTimer("Checking inputs", self._log.debug):
                try:
                    clipID_or_IDs_set: FrozenSet[int] = frozenset(
                        int(e) for e in clipID_or_IDs)
                except Exception as ex:
                    raise ValueError("Not all clip IDs could be used as ints: "
                                     "%s" % str(ex))

                id_diff = clipID_or_IDs_set.difference(self._row_id_index_map)
                assert not id_diff, \
                    "Not all clip IDs provided are represented in this " \
                    "distance kernel matrix! (difference: %s)" \
                    % id_diff
                del id_diff

            # Reorder the given clip IDs so that they are in the same relative
            # order as the kernel matrix edge order
            with SimpleTimer("Creating focus index/cid sequence",
                             self._log.debug):
                focus_row_indices = []
                focus_row_clipids = []
                for idx, cid in enumerate(self._row_id_index_map):
                    # if ((cid in clipID_or_IDs_set)
                    #         and (cid not in focus_row_clipids)):
                    if cid in clipID_or_IDs_set:
                        focus_row_indices.append(idx)
                        focus_row_clipids.append(cid)

            with SimpleTimer("Cropping kernel to focus range",
                             self._log.debug):
                return (tuple(focus_row_clipids),
                        tuple(self._col_id_index_map),
                        self._kernel[focus_row_indices, :])
Exemplo n.º 2
0
 def cache_table(self):
     """
     If a file cache has been specified, cache the current memory table state
     to the cache. Otherwise, this function does nothing.
     """
     if self._file_cache:
         with SimpleTimer("Caching memory table", self._log.debug):
             with open(self._file_cache, 'wb') as f:
                 cPickle.dump(self._table, f)
Exemplo n.º 3
0
 def cache_table(self):
     if self._file_cache:
         with SimpleTimer("Caching memory table", self._log.debug):
             with open(self._file_cache, 'wb') as f:
                 cPickle.dump(self._table, f)