Пример #1
0
def copy(src, dst, overwrite=True, verbose=True):
    import utool as ut
    if ut.isiterable(src) and not ut.isiterable(dst):
        ut.copy_files_to(src, dst, overwrite=overwrite, verbose=verbose)
        # list to non list
        pass
    else:
        return copy_single(src, dst, overwrite=overwrite, verbose=verbose)
Пример #2
0
def get_match_status(ibs, aid_pair):
    """ Data role for status column """
    aid1, aid2 = aid_pair
    assert not ut.isiterable(aid1), 'aid1=%r, aid2=%r' % (aid1, aid2)
    assert not ut.isiterable(aid2), 'aid1=%r, aid2=%r' % (aid1, aid2)
    text = ibs.get_match_text(aid1, aid2)
    if text is None:
        raise AssertionError('impossible state id_review_api')
    return text
Пример #3
0
 def get_pair_tags(edge):
     aid1, aid2 = edge
     assert not ut.isiterable(aid1), 'aid1=%r, aid2=%r' % (aid1, aid2)
     assert not ut.isiterable(aid2), 'aid1=%r, aid2=%r' % (aid1, aid2)
     am_rowids = ibs.get_annotmatch_rowid_from_undirected_superkey(
         [aid1], [aid2])
     tag_text = ibs.get_annotmatch_tag_text(am_rowids)[0]
     if tag_text is None:
         tag_text = ''
     return str(tag_text)
Пример #4
0
def ensure_flatiterable(input_):
    if isinstance(input_, six.string_types):
        input_ = ut.fuzzy_int(input_)
    if isinstance(input_, int) or not ut.isiterable(input_):
        return [input_]
    elif isinstance(input_, (list, tuple)):
        #print(input_)
        if len(input_) > 0 and ut.isiterable(input_[0]):
            return ut.flatten(input_)
        return input_
    else:
        raise TypeError('cannot ensure %r input_=%r is iterable', (type(input_), input_))
Пример #5
0
def ensure_flatiterable(input_):
    if isinstance(input_, six.string_types):
        input_ = ut.fuzzy_int(input_)
    if isinstance(input_, int) or not ut.isiterable(input_):
        return [input_]
    elif isinstance(input_, (list, tuple)):
        #print(input_)
        if len(input_) > 0 and ut.isiterable(input_[0]):
            return ut.flatten(input_)
        return input_
    else:
        raise TypeError('cannot ensure %r input_=%r is iterable', (type(input_), input_))
Пример #6
0
def get_status(ibs, aid_pair):
    """ Data role for status column
    FIXME: no other function in this project takes a tuple of scalars as an
    argument. Everything else is written in the context of lists, This function
    should follow the same paradigm, but CustomAPI will have to change.
    """
    aid1, aid2 = aid_pair
    assert not utool.isiterable(aid1), 'aid1=%r, aid2=%r' % (aid1, aid2)
    assert not utool.isiterable(aid2), 'aid1=%r, aid2=%r' % (aid1, aid2)
    #text  = ibsfuncs.vsstr(aid1, aid2)
    text = ibs.get_match_text(aid1, aid2)
    if text is None:
        raise AssertionError('impossible state inspect_gui')
    return text
Пример #7
0
def get_reviewed_status(ibs, aid_pair):
    """ Data role for status column """
    aid1, aid2 = aid_pair
    assert not ut.isiterable(aid1), 'aid1=%r, aid2=%r' % (aid1, aid2)
    assert not ut.isiterable(aid2), 'aid1=%r, aid2=%r' % (aid1, aid2)
    # FIXME: use new api
    state = ibs.get_annot_pair_is_reviewed([aid1], [aid2])[0]
    state_to_text = {
        None: 'Unreviewed',
        2: 'Auto-reviewed',
        1: 'User-reviewed',
    }
    default = '??? unknown mode %r' % (state,)
    text = state_to_text.get(state, default)
    return text
Пример #8
0
def get_annot_texts(ibs, aid_list, **kwargs):
    """ Add each type of text_list to the strings list """
    try:
        ibsfuncs.assert_valid_aids(ibs, aid_list)
        assert utool.isiterable(aid_list), 'input must be iterable'
        assert all([isinstance(aid, int) for aid in aid_list]), 'invalid input'
    except AssertionError as ex:
        utool.printex(ex, 'invalid input', 'viz', key_list=['aid_list'])
        raise
    texts_list = []  # list of lists of texts
    if kwargs.get('show_aidstr', True):
        aidstr_list = get_aidstrs(aid_list)
        texts_list.append(aidstr_list)
    if kwargs.get('show_gname', False):
        gname_list = ibs.get_annot_gnames(aid_list)
        texts_list.append(['gname=%s' % gname for gname in gname_list])
    if kwargs.get('show_name', True):
        name_list = ibs.get_annot_names(aid_list)
        texts_list.append(['name=%s' % name for name in name_list])
    if kwargs.get('show_exemplar', True):
        flag_list = ibs.get_annot_exemplar_flag(aid_list)
        texts_list.append(['EX' if flag else '' for flag in flag_list])
    # zip them up to get a tuple for each chip and join the fields
    if len(texts_list) > 0:
        annotation_text_list = [', '.join(tup) for tup in izip(*texts_list)]
    else:
        # no texts were specified return empty string for each input
        annotation_text_list = [''] * len(aid_list)
    return annotation_text_list
Пример #9
0
    def view(self, rowids):
        """
        returns a view of a view that uses the same per-item cache

        Example:
            >>> # ENABLE_DOCTEST
            >>> from ibeis._ibeis_object import *  # NOQA
            >>> import ibeis
            >>> ibs = ibeis.opendb(defaultdb='testdb1')
            >>> aids = ibs.get_valid_aids()
            >>> annots = ibs.annots(aids)
            >>> self = annots.view(annots._rowids)
            >>> v1 = self.view([1, 1, 2, 3, 1, 2])
            >>> v2 = self.view([3, 4, 5])
            >>> v3 = self.view([1, 4])
            >>> v4 = self.view(3)
            >>> lazy4 = v4._make_lazy_dict()
            >>> assert v1.vecs[0] is v3.vecs[0]
            >>> assert v2._cache is self._cache
            >>> assert v2._cache is v1._cache
        """
        if ut.isiterable(rowids):
            childview = self.__class__(rowids,
                                       obj1d=self._obj1d,
                                       cache=self._cache)
        else:
            childview = self.__class__([rowids],
                                       obj1d=self._obj1d,
                                       cache=self._cache)
            childview = ObjectScalar0D(childview)
        return childview
Пример #10
0
    def shallowcopy(qreq_, qaids=None):
        """
        Creates a copy of qreq with the same qparams object and a subset of the
        qx and dx objects.  used to generate chunks of vsmany queries

        CommandLine:
            python -m wbia.algo.hots.query_request QueryRequest.shallowcopy

        Example:
            >>> # ENABLE_DOCTEST
            >>> from wbia.algo.hots.query_request import *  # NOQA
            >>> import wbia
            >>> qreq_ = wbia.testdata_qreq_(default_qaids=[1, 2])
            >>> qreq2_ = qreq_.shallowcopy(qaids=1)
            >>> assert qreq_.daids is qreq2_.daids, 'should be the same'
            >>> assert len(qreq_.qaids) != len(qreq2_.qaids), 'should be diff'
            >>> #assert qreq_.metadata is not qreq2_.metadata
        """
        # qreq2_ = copy.copy(qreq_)  # copy calls setstate and getstate
        qreq2_ = QueryRequest()
        qreq2_.__dict__.update(qreq_.__dict__)
        qaids = [qaids] if not ut.isiterable(qaids) else qaids
        _intersect = np.intersect1d(qaids, qreq2_.qaids)
        assert len(_intersect) == len(qaids), 'not a subset'
        qreq2_.set_external_qaids(qaids)
        # The shallow copy does not bring over output / query data
        qreq2_.indexer = None
        # qreq2_.metadata = {}
        qreq2_.hasloaded = False
        return qreq2_
Пример #11
0
 def __getitem__(self, idx):
     if isinstance(idx, slice):
         idxs = list(range(*idx.indices(len(self))))
         return self.take(idxs)
     if not ut.isiterable(idx):
         obj0d_ = self.take([idx])
         obj0d = ObjectScalar0D(obj0d_)
         return obj0d
     if not isinstance(idx, slice):
         raise AssertionError('only slice supported currently')
     return self.take(idx)
Пример #12
0
def overwrite_annot_case_tags(ibs, aid_list, tag_list):
    """
    Completely replaces annotation tags.
    BE VERY CAREFUL WITH THIS FUNCTION
    """
    assert all([ut.isiterable(tag) for tag in tag_list])
    # text_list = ibs.get_annot_tag_text(aid_list)
    # orig_tags_list = [[] if note is None else _parse_tags(note) for note in text_list]
    new_tags_list = tag_list
    new_text_list = [';'.join(tags) for tags in new_tags_list]
    ibs.set_annot_tag_text(aid_list, new_text_list)
Пример #13
0
def set_part_quality_texts(ibs, part_rowid_list, quality_text_list):
    r"""
    Auto-docstr for 'set_part_quality_texts'

    RESTful:
        Method: PUT
        URL:    /api/part/quality/text/
    """
    if not ut.isiterable(part_rowid_list):
        part_rowid_list = [part_rowid_list]
    if isinstance(quality_text_list, six.string_types):
        quality_text_list = [quality_text_list]
    quality_list = ut.dict_take(const.QUALITY_TEXT_TO_INT, quality_text_list)
    ibs.set_part_qualities(part_rowid_list, quality_list)
Пример #14
0
def cast_into_qt(data):
    """
    Casts python data into a representation suitable for QT (usually a string)
    """
    if SIMPLE_CASTING:
        if ut.is_str(data):
            return __STR__(data)
        elif ut.is_float(data):
            # qnumber = QString.number(float(data), format='g', precision=8)
            return locale_float(data)
        elif ut.is_bool(data):
            return bool(data)
        elif ut.is_int(data):
            return int(data)
        elif isinstance(data, uuid.UUID):
            return __STR__(data)
        elif ut.isiterable(data):
            return ', '.join(map(__STR__, data))
        else:
            return __STR__(data)
    if ut.is_str(data):
        return __STR__(data)
    elif ut.is_float(data):
        # qnumber = QString.number(float(data), format='g', precision=8)
        return locale_float(data)
    elif ut.is_bool(data):
        return bool(data)
    elif ut.is_int(data):
        return int(data)
    elif isinstance(data, uuid.UUID):
        return __STR__(data)
    elif ut.isiterable(data):
        return ', '.join(map(__STR__, data))
    elif data is None:
        return 'None'
    else:
        return 'Unknown qtype: %r for data=%r' % (type(data), data)
Пример #15
0
def cast_into_qt(data):
    """
    Casts python data into a representation suitable for QT (usually a string)
    """
    if SIMPLE_CASTING:
        if ut.is_str(data):
            return __STR__(data)
        elif ut.is_float(data):
            #qnumber = QString.number(float(data), format='g', precision=8)
            return locale_float(data)
        elif ut.is_bool(data):
            return bool(data)
        elif  ut.is_int(data):
            return int(data)
        elif isinstance(data, uuid.UUID):
            return __STR__(data)
        elif ut.isiterable(data):
            return ', '.join(map(__STR__, data))
        else:
            return __STR__(data)
    if ut.is_str(data):
        return __STR__(data)
    elif ut.is_float(data):
        #qnumber = QString.number(float(data), format='g', precision=8)
        return locale_float(data)
    elif ut.is_bool(data):
        return bool(data)
    elif  ut.is_int(data):
        return int(data)
    elif isinstance(data, uuid.UUID):
        return __STR__(data)
    elif ut.isiterable(data):
        return ', '.join(map(__STR__, data))
    elif data is None:
        return 'None'
    else:
        return 'Unknown qtype: %r for data=%r' % (type(data), data)
Пример #16
0
    def generate_class_images(dream, target_labels):
        """
        import plottool as pt
        fnum = None
        kw = dict(init='gauss', niters=500, update_rate=.05, weight_decay=1e-4)
        target_labels = list(range(model.output_dims))
        dream = draw_net.Dream(model, **kw)
        target_labels = 8
        images = list(dream.generate_class_images(target_labels))

        vid = vt.make_video(images, 'dynimg.pimj', fps=1, is_color=False, format='PIM1')
        vid = vt.make_video2(images, 'dynimg')

        import matplotlib.pyplot as plt
        ims = []
        for img in imgs:
            im = plt.imshow(img[:, :, 0], interpolation='nearest', cmap='gray')
            ims.append([im])

        import matplotlib.animation as animation
        fig = plt.figure()
        ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                        repeat_delay=1000)
        ani.save('dynamic_images.mp4')
        ut.startfile('dynamic_images.mp4')
        plt.show()
        """
        import ibeis_cnn.__THEANO__ as theano
        from ibeis_cnn.__THEANO__ import tensor as T  # NOQA
        import utool as ut
        input_shape = dream.model.input_shape
        b, c, w, h = input_shape
        was_scalar = not ut.isiterable(target_labels)
        target_labels = ut.ensure_iterable(target_labels)
        assert len(target_labels) <= b, 'batch size too small'
        initial_state = dream._make_init_state()
        shared_images = theano.shared(initial_state.astype(np.float32))
        step_fn = dream._make_objective(shared_images, target_labels)
        out = dream._postprocess_class_image(shared_images, target_labels,
                                             was_scalar)
        yield out
        for _ in ut.ProgIter(range(dream.niters), lbl='class dream',
                             bs=True):
            step_fn()
            # objective = step_fn()
            # print('objective = %r' % (objective,))
            out = dream._postprocess_class_image(shared_images, target_labels,
                                                 was_scalar)
            yield out
Пример #17
0
 def param_distribution(param_pdf, rng=rng):
     if param_pdf is None:
         param = 0
     elif not ut.isiterable(param_pdf):
         max_param = param_pdf
         if scalar_anchor == 'reflect':
             min_param = -max_param
         elif scalar_anchor == 0:
             min_param = 0
         param = rng.uniform(min_param, max_param)
     elif isinstance(param_pdf, tuple):
         min_param, max_param = param_pdf
         param = rng.uniform(min_param, max_param)
     else:
         assert False
     return param
Пример #18
0
 def param_distribution(param_pdf, rng=rng):
     if param_pdf is None:
         param = 0
     elif not ut.isiterable(param_pdf):
         max_param = param_pdf
         if scalar_anchor == 'reflect':
             min_param = -max_param
         elif scalar_anchor == 0:
             min_param = 0
         param = rng.uniform(min_param, max_param)
     elif isinstance(param_pdf, tuple):
         min_param, max_param = param_pdf
         param = rng.uniform(min_param, max_param)
     else:
         assert False
     return param
Пример #19
0
 def get(self, column, row):
     """ getters always receive primary rowids, rectify if col_ider is
     specified (row might be a row_pair) """
     index = self._infer_index(column, row)
     column_getter = self.col_getter_list[column]
     # Columns might be getter funcs indexable read/write arrays
     try:
         return utool.general_get(column_getter, index)
     except Exception:
         # FIXME: There may be an issue on tuple-key getters when row input is
         # vectorized. Hack it away
         if utool.isiterable(row):
             row_list = row
             return [self.get(column, row_) for row_ in row_list]
         else:
             raise
Пример #20
0
def get_textformat_tag_flags(prop, text_list):
    """ general text tag getter hack """
    tags_list = [
        None if note is None else _parse_tags(note) for note in text_list
    ]
    if ut.isiterable(prop):
        props_ = [p.lower() for p in prop]
        flags_list = [[
            None if tags is None else int(prop_ in tags) for tags in tags_list
        ] for prop_ in props_]
        return flags_list
    else:
        prop = prop.lower()
        flag_list = [
            None if tags is None else int(prop in tags) for tags in tags_list
        ]
        return flag_list
Пример #21
0
 def get(self, column, row, **kwargs):
     """
     getters always receive primary rowids, rectify if col_ider is
     specified (row might be a row_pair)
     """
     index = self._infer_index(column, row)
     column_getter = self.col_getter_list[column]
     # Columns might be getter funcs indexable read/write arrays
     try:
         return self._general_get(column_getter, index, **kwargs)
     except Exception:
         # FIXME: There may be an issue on tuple-key getters when row input is
         # vectorized. Hack it away
         if ut.isiterable(row):
             row_list = row
             return [self.get(column, row_, **kwargs) for row_ in row_list]
         else:
             raise
Пример #22
0
    def shallowcopy(qreq_, qaids=None, qx=None, dx=None):
        """
        Creates a copy of qreq with the same qparams object and a subset of the
        qx and dx objects.  used to generate chunks of vsone and vsmany queries

        CommandLine:
            python -m ibeis.algo.hots.query_request --exec-shallowcopy

        Example:
            >>> # ENABLE_DOCTEST
            >>> from ibeis.algo.hots.query_request import *  # NOQA
            >>> import ibeis
            >>> qreq_, ibs = testdata_qreq()
            >>> qreq2_ = qreq_.shallowcopy(qx=0)
            >>> assert qreq_.get_external_daids() is qreq2_.get_external_daids()
            >>> assert len(qreq_.get_external_qaids()) != len(qreq2_.get_external_qaids())
            >>> #assert qreq_.metadata is not qreq2_.metadata
        """
        #qreq2_ = copy.copy(qreq_)  # copy calls setstate and getstate
        qreq2_ = QueryRequest()
        qreq2_.__dict__.update(qreq_.__dict__)
        if qx is not None:
            qaid_list  = qreq2_.get_external_qaids()
            qaid_list  = qaid_list[qx:qx + 1]
            qreq2_.set_external_qaids(qaid_list)  # , quuid_list)
        elif qaids is not None:
            assert qx is None, 'cannot specify both qx and qaids'
            qaids = [qaids] if not ut.isiterable(qaids) else qaids
            _intersect = np.intersect1d(qaids, qreq2_.get_external_qaids())
            assert len(_intersect) == len(qaids), 'not a subset'
            qreq2_.set_external_qaids(qaids)  # , quuid_list)
        if dx is not None:
            daid_list  = qreq2_.get_external_daids()
            daid_list  = daid_list[dx:dx + 1]
            #duuid_list = qreq2_.get_external_duuids()
            #duuid_list = duuid_list[dx:dx + 1]
            qreq2_.set_external_daids(daid_list)

        # The shallow copy does not bring over output / query data
        qreq2_.indexer = None
        #qreq2_.metadata = {}
        qreq2_.hasloaded = False
        return qreq2_
Пример #23
0
    def shallowcopy(qreq_, qaids=None, qx=None, dx=None):
        """
        Creates a copy of qreq with the same qparams object and a subset of the
        qx and dx objects.  used to generate chunks of vsone and vsmany queries

        CommandLine:
            python -m ibeis.algo.hots.query_request --exec-shallowcopy

        Example:
            >>> # ENABLE_DOCTEST
            >>> from ibeis.algo.hots.query_request import *  # NOQA
            >>> import ibeis
            >>> qreq_, ibs = testdata_qreq()
            >>> qreq2_ = qreq_.shallowcopy(qx=0)
            >>> assert qreq_.get_external_daids() is qreq2_.get_external_daids()
            >>> assert len(qreq_.get_external_qaids()) != len(qreq2_.get_external_qaids())
            >>> #assert qreq_.metadata is not qreq2_.metadata
        """
        #qreq2_ = copy.copy(qreq_)  # copy calls setstate and getstate
        qreq2_ = QueryRequest()
        qreq2_.__dict__.update(qreq_.__dict__)
        if qx is not None:
            qaid_list  = qreq2_.get_external_qaids()
            qaid_list  = qaid_list[qx:qx + 1]
            qreq2_.set_external_qaids(qaid_list)  # , quuid_list)
        elif qaids is not None:
            assert qx is None, 'cannot specify both qx and qaids'
            qaids = [qaids] if not ut.isiterable(qaids) else qaids
            _intersect = np.intersect1d(qaids, qreq2_.get_external_qaids())
            assert len(_intersect) == len(qaids), 'not a subset'
            qreq2_.set_external_qaids(qaids)  # , quuid_list)
        if dx is not None:
            daid_list  = qreq2_.get_external_daids()
            daid_list  = daid_list[dx:dx + 1]
            #duuid_list = qreq2_.get_external_duuids()
            #duuid_list = duuid_list[dx:dx + 1]
            qreq2_.set_external_daids(daid_list)

        # The shallow copy does not bring over output / query data
        qreq2_.indexer = None
        #qreq2_.metadata = {}
        qreq2_.hasloaded = False
        return qreq2_
Пример #24
0
    def load_cached_chipmatch(qreq_, qaid=None):
        """
        DEPRICATE in favor of chipmatch

        convinience function for loading a query that has already been
        cached """
        if qaid is None:
            qaid = qreq_.get_external_qaids()
        shallow_qreq_ = qreq_.shallowcopy()
        is_scalar = not ut.isiterable(qaid)
        qaid_list = [qaid] if is_scalar else qaid
        shallow_qreq_.set_external_qaids(qaid_list)
        cm_list = shallow_qreq_.ibs.query_chips(
            qaid_list, qreq_.get_external_daids(), use_cache=True,
            use_bigcache=False, qreq_=shallow_qreq_)
        if is_scalar:
            return cm_list[0]
        else:
            return cm_list
Пример #25
0
    def make_class_images(dream, target_labels):
        import ibeis_cnn.__THEANO__ as theano
        from ibeis_cnn.__THEANO__ import tensor as T  # NOQA
        import utool as ut

        was_scalar = not ut.isiterable(target_labels)
        target_labels = ut.ensure_iterable(target_labels)

        if True:
            # We are forcing a batch size for this visualization
            input_shape = (len(target_labels),) + dream.model.input_shape[1:]
        else:
            # Maybe some cnn layers cant take variable batches?
            input_shape = dream.model.input_shape
        b, c, w, h = input_shape
        assert len(target_labels) <= b, 'batch size too small'

        initial_state = dream._make_init_state()

        # make image a shared variable that you can update
        if dream.shared_images is None:
            dream.shared_images = theano.shared(initial_state)
        else:
            dream.shared_images.set_value(initial_state)

        if dream.step_fn is None:
            dream.step_fn = dream._make_objective(dream.shared_images, target_labels)

        # Optimize objective via backpropogation for a few iterations
        for _ in ut.ProgIter(range(dream.niters), lbl='making class model img',
                             bs=True):
            dream.step_fn()
            #print('objective = %r' % (objective,))

        out = dream._postprocess_class_image(dream.shared_images, target_labels, was_scalar)
        return out
Пример #26
0
def get_aidstrs(aid_list, **kwargs):
    if ut.isiterable(aid_list):
        return [ibsfuncs.aidstr(aid, **kwargs) for aid in aid_list]
    else:
        return ibsfuncs.aidstr(aid_list, **kwargs)
Пример #27
0
 def wrapper(input_):
     if not ut.isiterable(input_):
         return func(si_func(input_))
     else:
         return list(map(func, si_func(input_)))
Пример #28
0
def interpolate_replbounds(xdata, ydata, pt):
    """
    xdata = np.array([.1, .2, .3, .4, .5])
    ydata = np.array([.1, .2, .3, .4, .5])
    pt = .35

    FIXME:
        if duplicate xdata is given bad things happen.
    BUG:
        in scipy.interpolate.interp1d
        If there is a duplicate xdata, then assume_sorted=False will
        sort ydata by xdata, but xdata should retain its initial ordering
        in places of ambuguity. Currently it does not.
    Args:
        xdata (ndarray):
        ydata (ndarray):
        pt (ndarray):

    Returns:
        float: interp_vals

    CommandLine:
        python -m vtool.confusion --exec-interpolate_replbounds

    Example:
        >>> # DISABLE_DOCTEST
        >>> from vtool.confusion import *  # NOQA
        >>> xdata = np.array([0.7,  0.8,  0.8,  0.9,  0.9, 0.9])
        >>> ydata = np.array([34,    26,   23,   22,   19,  17])
        >>> pt = np.array([.85, 1.0, -1.0])
        >>> interp_vals = interpolate_replbounds(xdata, ydata, pt)
        >>> result = ('interp_vals = %s' % (str(interp_vals),))
        >>> print(result)
        interp_vals = [ 22.5  17.   34. ]
    """
    if not ut.issorted(xdata):
        raise AssertionError('need to sort xdata and ydata in function')
        sortx = np.lexsort(np.vstack([np.arange(len(xdata)), xdata]))
        xdata = xdata.take(sortx, axis=0)
        ydata = ydata.take(sortx, axis=0)

    is_scalar = not ut.isiterable(pt)
    #print('----')
    #print('xdata = %r' % (xdata,))
    #print('ydata = %r' % (ydata,))
    if is_scalar:
        pt = np.array([pt])
    #ut.ensure_iterable(pt)
    minval = xdata.min()
    maxval = xdata.max()
    argx_min_list = np.argwhere(xdata == minval)
    argx_max_list = np.argwhere(xdata == maxval)
    argx_min = argx_min_list.min()
    argx_max = argx_max_list.max()
    lower_mask = pt < xdata[argx_min]
    upper_mask = pt > xdata[argx_max]
    interp_mask = ~np.logical_or(lower_mask, upper_mask)
    #if isinstance(pt, np.ndarray):
    dtype = np.result_type(np.float32, ydata.dtype)
    interp_vals = np.empty(pt.shape, dtype=dtype)
    interp_vals[lower_mask] = ydata[argx_min]
    interp_vals[upper_mask] = ydata[argx_max]
    if np.any(interp_mask):
        # FIXME: allow assume_sorted = False
        func = scipy.interpolate.interp1d(xdata,
                                          ydata,
                                          kind='linear',
                                          assume_sorted=True)
        interp_vals[interp_mask] = func(pt[interp_mask])
    if is_scalar:
        interp_vals = interp_vals[0]
    # interpolate to target recall
    #right_index  = indicies[0]
    #right_recall = self.recall[right_index]
    #left_index   = right_index - 1
    #left_recall  = self.recall[left_index]
    #stepsize = right_recall - left_recall
    #alpha = (target_recall - left_recall) / stepsize
    #left_fpr   = self.fpr[left_index]
    #right_fpr  = self.fpr[right_index]
    #interp_fpp = (left_fpr * (1 - alpha)) + (right_fpr * (alpha))
    return interp_vals
Пример #29
0
    def new_cpd(self, parents=None, pmf_func=None):
        """
        Makes a new random variable that is an instance of this tempalte

        parents : only used to define the name of this node.
        """
        if pmf_func is None:
            pmf_func = self.pmf_func

        # --- MAKE VARIABLE ID
        def _getid(obj):
            if isinstance(obj, int):
                return str(obj)
            elif isinstance(obj, six.string_types):
                return obj
            else:
                return obj._template_id

        if not ut.isiterable(parents):
            parents = [parents]

        template_ids = [_getid(cpd) for cpd in parents]
        HACK_SAME_IDS = True
        # TODO: keep track of parent index inheritence
        # then rectify uniqueness based on that
        if HACK_SAME_IDS and ut.allsame(template_ids):
            _id = template_ids[0]
        else:
            _id = ''.join(template_ids)
        variable = ''.join([self.varpref, _id])
        # variable = '_'.join([self.varpref, '{' + _id + '}'])
        # variable = '$%s$' % (variable,)

        evidence_cpds = [cpd for cpd in parents if hasattr(cpd, 'ttype')]
        if len(evidence_cpds) == 0:
            evidence_cpds = None

        variable_card = len(self.basis)
        statename_dict = {
            variable: self.basis,
        }
        if self.evidence_ttypes is not None:
            if any(cpd.ttype != tcpd.ttype
                   for cpd, tcpd in zip(evidence_cpds, evidence_cpds)):
                raise ValueError('Evidence is not of appropriate type')
            evidence_bases = [cpd.variable_statenames for cpd in evidence_cpds]
            evidence_card = list(map(len, evidence_bases))
            evidence_states = list(ut.iprod(*evidence_bases))

            for cpd in evidence_cpds:
                _dict = ut.dict_subset(cpd.statename_dict, [cpd.variable])
                statename_dict.update(_dict)

            evidence = [cpd.variable for cpd in evidence_cpds]
        else:
            if evidence_cpds is not None:
                raise ValueError('Gave evidence for evidence-less template')
            evidence = None
            evidence_card = None

        # --- MAKE TABLE VALUES
        if pmf_func is not None:
            if isinstance(pmf_func, list):
                values = np.array(pmf_func)
            else:
                values = np.array([[
                    pmf_func(vstate, *estates) for estates in evidence_states
                ] for vstate in self.basis])
            ensure_normalized = True
            if ensure_normalized:
                values = values / values.sum(axis=0)
        else:
            # assume uniform
            fill_value = 1.0 / variable_card
            if evidence_card is None:
                values = np.full((1, variable_card), fill_value)
            else:
                values = np.full([variable_card] + list(evidence_card),
                                 fill_value)

        try:
            cpd = pgmpy.factors.TabularCPD(
                variable=variable,
                variable_card=variable_card,
                values=values,
                evidence=evidence,
                evidence_card=evidence_card,
                # statename_dict=statename_dict,
                state_names=statename_dict,
            )
        except Exception as ex:
            ut.printex(
                ex,
                'Failed to create TabularCPD',
                keys=[
                    'variable',
                    'variable_card',
                    'statename_dict',
                    'evidence_card',
                    'evidence',
                    'values.shape',
                ],
            )
            ut.embed()
            raise

        cpd.ttype = self.ttype
        cpd._template_ = self
        cpd._template_id = _id
        return cpd
Пример #30
0
    def new_cpd(self, parents=None, pmf_func=None):
        """
        Makes a new random variable that is an instance of this tempalte

        parents : only used to define the name of this node.
        """
        if pmf_func is None:
            pmf_func = self.pmf_func

        # --- MAKE VARIABLE ID
        def _getid(obj):
            if isinstance(obj, int):
                return str(obj)
            elif isinstance(obj, six.string_types):
                return obj
            else:
                return obj._template_id

        if not ut.isiterable(parents):
            parents = [parents]

        template_ids = [_getid(cpd) for cpd in parents]
        HACK_SAME_IDS = True
        # TODO: keep track of parent index inheritence
        # then rectify uniqueness based on that
        if HACK_SAME_IDS and ut.list_allsame(template_ids):
            _id = template_ids[0]
        else:
            _id = ''.join(template_ids)
        variable = ''.join([self.varpref, _id])
        #variable = '_'.join([self.varpref, '{' + _id + '}'])
        #variable = '$%s$' % (variable,)

        evidence_cpds = [cpd for cpd in parents if hasattr(cpd, 'ttype')]
        if len(evidence_cpds) == 0:
            evidence_cpds = None

        variable_card = len(self.basis)
        statename_dict = {
            variable: self.basis,
        }
        if self.evidence_ttypes is not None:
            if any(cpd.ttype != tcpd.ttype
                   for cpd, tcpd in zip(evidence_cpds, evidence_cpds)):
                raise ValueError('Evidence is not of appropriate type')
            evidence_bases = [cpd.variable_statenames for cpd in evidence_cpds]
            evidence_card = list(map(len, evidence_bases))
            evidence_states = list(ut.iprod(*evidence_bases))

            for cpd in evidence_cpds:
                _dict = ut.dict_subset(cpd.statename_dict, [cpd.variable])
                statename_dict.update(_dict)

            evidence = [cpd.variable for cpd in evidence_cpds]
        else:
            if evidence_cpds is not None:
                raise ValueError('Gave evidence for evidence-less template')
            evidence = None
            evidence_card = None

        # --- MAKE TABLE VALUES
        if pmf_func is not None:
            if isinstance(pmf_func, list):
                values = np.array(pmf_func)
            else:
                values = np.array([
                    [pmf_func(vstate, *estates) for estates in evidence_states]
                    for vstate in self.basis
                ])
            ensure_normalized = True
            if ensure_normalized:
                values = values / values.sum(axis=0)
        else:
            # assume uniform
            fill_value = 1.0 / variable_card
            if evidence_card is None:
                values = np.full((1, variable_card), fill_value)
            else:
                values = np.full([variable_card] + list(evidence_card), fill_value)

        try:
            cpd = pgmpy.factors.TabularCPD(
                variable=variable,
                variable_card=variable_card,
                values=values,
                evidence=evidence,
                evidence_card=evidence_card,
                statename_dict=statename_dict,
            )
        except Exception as ex:
            ut.printex(ex, 'Failed to create TabularCPD',
                       keys=[
                           'variable',
                           'variable_card',
                           'statename_dict',
                           'evidence_card',
                           'evidence',
                           'values.shape',
                       ])
            raise

        cpd.ttype = self.ttype
        cpd._template_ = self
        cpd._template_id = _id
        return cpd
Пример #31
0
def _annotate_kpts(kpts_, sel_fx=None, **kwargs):
    r"""
    Args:
        kpts_ (ndarray): keypoints
        sel_fx (None):

    Keywords:
        color:  3/4-tuple, ndarray, or str

    Returns:
        None

    Example:
        >>> from plottool.viz_keypoints import *  # NOQA
        >>> sel_fx = None
        >>> kpts = np.array([[  92.9246,   17.5453,    7.8103,   -3.4594,   10.8566,    0.    ],
        ...                  [  76.8585,   24.7918,   11.4412,   -3.2634,    9.6287,    0.    ],
        ...                  [ 140.6303,   24.9027,   10.4051,  -10.9452, 10.5991,    0.    ],])

    """
    if len(kpts_) == 0:
        print('len(kpts_) == 0...')
        return
    #color = kwargs.get('color', 'distinct' if sel_fx is None else df2.ORANGE)
    color = kwargs.get('color', 'scale' if sel_fx is None else df2.ORANGE)
    if color == 'distinct':
        # hack for distinct colors
        color = df2.distinct_colors(len(kpts_))  # , randomize=True)
    elif color == 'scale':
        # hack for distinct colors
        import vtool as vt
        #color = df2.scores_to_color(vt.get_scales(kpts_), cmap_='inferno', score_range=(0, 50))
        color = df2.scores_to_color(vt.get_scales(kpts_), cmap_='viridis', score_range=(5, 30), cmap_range=None)
        #df2.distinct_colors(len(kpts_))  # , randomize=True)
    # Keypoint drawing kwargs
    drawkpts_kw = {
        'ell': True,
        'pts': False,
        'ell_alpha': .4,
        'ell_linewidth': 2,
        'ell_color': color,
    }
    drawkpts_kw.update(kwargs)

    # draw all keypoints
    if sel_fx is None:
        df2.draw_kpts2(kpts_, **drawkpts_kw)
    else:
        # dont draw the selected keypoint in this batch
        nonsel_kpts_ = np.vstack((kpts_[0:sel_fx], kpts_[sel_fx + 1:]))
        # Draw selected keypoint
        sel_kpts = kpts_[sel_fx:sel_fx + 1]
        import utool as ut
        if ut.isiterable(color) and ut.isiterable(color[0]):
            # hack for distinct colors
            drawkpts_kw['ell_color'] = color[0:sel_fx] + color[sel_fx + 1:]
        drawkpts_kw
        drawkpts_kw2 = drawkpts_kw.copy()
        drawkpts_kw2.update({
            'ell_color': df2.BLUE,
            'eig':  True,
            'rect': True,
            'ori':  True,
        })
        df2.draw_kpts2(nonsel_kpts_, **drawkpts_kw)
        df2.draw_kpts2(sel_kpts, **drawkpts_kw2)
Пример #32
0
def interpolate_replbounds(xdata, ydata, pt):
    """
    xdata = np.array([.1, .2, .3, .4, .5])
    ydata = np.array([.1, .2, .3, .4, .5])
    pt = .35

    FIXME:
        if duplicate xdata is given bad things happen.
    BUG:
        in scipy.interpolate.interp1d
        If there is a duplicate xdata, then assume_sorted=False will
        sort ydata by xdata, but xdata should retain its initial ordering
        in places of ambuguity. Currently it does not.
    Args:
        xdata (ndarray):
        ydata (ndarray):
        pt (ndarray):

    Returns:
        float: interp_vals

    CommandLine:
        python -m vtool.confusion --exec-interpolate_replbounds

    Example:
        >>> # DISABLE_DOCTEST
        >>> from vtool.confusion import *  # NOQA
        >>> xdata = np.array([0.7,  0.8,  0.8,  0.9,  0.9, 0.9])
        >>> ydata = np.array([34,    26,   23,   22,   19,  17])
        >>> pt = np.array([.85, 1.0, -1.0])
        >>> interp_vals = interpolate_replbounds(xdata, ydata, pt)
        >>> result = ('interp_vals = %s' % (str(interp_vals),))
        >>> print(result)
        interp_vals = [ 22.5  17.   34. ]
    """
    if not ut.issorted(xdata):
        raise AssertionError("need to sort xdata and ydata in function")
        sortx = np.lexsort(np.vstack([np.arange(len(xdata)), xdata]))
        xdata = xdata.take(sortx, axis=0)
        ydata = ydata.take(sortx, axis=0)

    is_scalar = not ut.isiterable(pt)
    # print('----')
    # print('xdata = %r' % (xdata,))
    # print('ydata = %r' % (ydata,))
    if is_scalar:
        pt = np.array([pt])
    # ut.ensure_iterable(pt)
    minval = xdata.min()
    maxval = xdata.max()
    argx_min_list = np.argwhere(xdata == minval)
    argx_max_list = np.argwhere(xdata == maxval)
    argx_min = argx_min_list.min()
    argx_max = argx_max_list.max()
    lower_mask = pt < xdata[argx_min]
    upper_mask = pt > xdata[argx_max]
    interp_mask = ~np.logical_or(lower_mask, upper_mask)
    # if isinstance(pt, np.ndarray):
    dtype = np.result_type(np.float32, ydata.dtype)
    interp_vals = np.empty(pt.shape, dtype=dtype)
    interp_vals[lower_mask] = ydata[argx_min]
    interp_vals[upper_mask] = ydata[argx_max]
    if np.any(interp_mask):
        # FIXME: allow assume_sorted = False
        func = scipy.interpolate.interp1d(xdata, ydata, kind="linear", assume_sorted=True)
        interp_vals[interp_mask] = func(pt[interp_mask])
    if is_scalar:
        interp_vals = interp_vals[0]
    # interpolate to target recall
    # right_index  = indicies[0]
    # right_recall = self.recall[right_index]
    # left_index   = right_index - 1
    # left_recall  = self.recall[left_index]
    # stepsize = right_recall - left_recall
    # alpha = (target_recall - left_recall) / stepsize
    # left_fpr   = self.fpr[left_index]
    # right_fpr  = self.fpr[right_index]
    # interp_fpp = (left_fpr * (1 - alpha)) + (right_fpr * (alpha))
    return interp_vals
Пример #33
0
def get_table_rows(cur, tablename, colnames, where=None, params=None, unpack=True):
    import utool as ut
    want_single_column = isinstance(colnames, six.string_types)
    want_single_param = params is not None and not ut.isiterable(params)
    #isinstance(params, six.string_types)
    if want_single_column:
        colnames = (colnames,)
    if colnames is not None and colnames != '*':
        assert isinstance(colnames, tuple), 'colnames must be a tuple'
        colnames_str = ', '.join(colnames)
    else:
        colnames_str = '*'
    #if isinstance(colnames, six.string_types):
    #    colnames = (colnames,)
    fmtdict = {
        'tablename'     : tablename,
        'colnames'    : colnames_str,
        'orderby'     : '',
    }

    #ORDER BY rowid ASC
    if where is None:
        operation_fmt = '''
        SELECT {colnames}
        FROM {tablename}
        {orderby}
        '''
    else:
        fmtdict['where_clause'] = where
        operation_fmt = '''
        SELECT {colnames}
        FROM {tablename}
        WHERE {where_clause}
        {orderby}
        '''
    operation_str = operation_fmt.format(**fmtdict)
    if params is None:
        cur.execute(operation_str)
        val_list = cur.fetchall()
    elif want_single_param:
        cur.execute(operation_str, (params,))
        val_list = cur.fetchall()
    else:
        # Execute many
        def executemany_scalar_generator(operation_str, params):
            for param in params:
                cur.execute(operation_str, param)
                vals = cur.fetchall()
                #assert len(vals) == 1, 'vals=%r, len(vals)=%r' % (vals, len(vals))
                yield vals
        val_list = list(executemany_scalar_generator(operation_str, params))

    if unpack:
        if want_single_column:
            # want a single value per parameter
            val_list = [val[0] for val in val_list]

        if want_single_param:
            # wants a single parameter
            assert len(val_list) == 1
            val_list = val_list[0]
    return val_list
Пример #34
0
def get_annot_texts(ibs, aid_list, **kwargs):
    """ Add each type of text_list to the strings list

    Args:
        ibs (IBEISController):  wbia controller object
        aid_list (int):  list of annotation ids

    Returns:
        list: annotation_text_list

    CommandLine:
        python -m wbia.viz.viz_helpers --test-get_annot_texts

    Example:
        >>> # ENABLE_DOCTEST
        >>> from wbia.viz.viz_helpers import *  # NOQA
        >>> import wbia
        >>> import collections
        >>> ibs = wbia.opendb('testdb1')
        >>> # Default all kwargs to true
        >>> class KwargsProxy(object):
        ...    def get(self, a, b):
        ...        return True
        >>> kwargs_proxy = KwargsProxy()
        >>> aid_list = ibs.get_valid_aids()[::3]
        >>> # execute function
        >>> annotation_text_list = get_annot_texts(ibs, aid_list, kwargs_proxy=kwargs_proxy)
        >>> # verify results
        >>> result = ut.repr2(annotation_text_list, nl=1)
        >>> print(result)
        [
            'aid1, gname=easy1.JPG, name=____, nid=-1, , nGt=0, quality=UNKNOWN, view=left',
            'aid4, gname=hard1.JPG, name=____, nid=-4, , nGt=0, quality=UNKNOWN, view=left',
            'aid7, gname=jeff.png, name=jeff, nid=3, EX, nGt=0, quality=UNKNOWN, view=unknown',
            'aid10, gname=occl2.JPG, name=occl, nid=5, EX, nGt=0, quality=UNKNOWN, view=left',
            'aid13, gname=zebra.jpg, name=zebra, nid=7, EX, nGt=0, quality=UNKNOWN, view=unknown',
        ]
    """
    # HACK FOR TEST
    if 'kwargs_proxy' in kwargs:
        kwargs = kwargs['kwargs_proxy']
    try:
        ibsfuncs.assert_valid_aids(ibs, aid_list)
        assert ut.isiterable(aid_list), 'input must be iterable'
        assert all([isinstance(aid, ut.VALID_INT_TYPES)
                    for aid in aid_list]), 'invalid input'
    except AssertionError as ex:
        ut.printex(ex, 'invalid input', 'viz', key_list=['aid_list'])
        raise
    texts_list = []  # list of lists of texts
    if kwargs.get('show_aidstr', True):
        aidstr_list = get_aidstrs(aid_list)
        texts_list.append(aidstr_list)
    if kwargs.get('show_gname', False):
        gname_list = ibs.get_annot_image_names(aid_list)
        texts_list.append(['gname=%s' % gname for gname in gname_list])
    if kwargs.get('show_name', True):
        name_list = ibs.get_annot_names(aid_list)
        texts_list.append(['name=%s' % name for name in name_list])
    if kwargs.get('show_nid', False):
        nid_list = ibs.get_annot_name_rowids(aid_list)
        texts_list.append(['nid=%d' % nid for nid in nid_list])
    if kwargs.get('show_exemplar', True):
        flag_list = ibs.get_annot_exemplar_flags(aid_list)
        texts_list.append(['EX' if flag else '' for flag in flag_list])
    if kwargs.get('show_num_gt', True):
        # FIXME: This should be num_groundtruth with respect to the currently
        # allowed annotations
        nGt_list = ibs.get_annot_num_groundtruth(aid_list)
        texts_list.append(['nGt=%r' % nGt for nGt in nGt_list])
    if kwargs.get('show_quality_text', False):
        qualtext_list = ibs.get_annot_quality_texts(aid_list)
        texts_list.append(
            list(map(lambda text: 'quality=%s' % text, qualtext_list)))
    if kwargs.get('show_viewcode', False):
        # FIXME: This should be num_groundtruth with respect to the currently
        # allowed annotations
        viewcode_list = ibs.get_annot_viewpoint_code(aid_list)
        texts_list.append(
            list(map(lambda text: 'view=%s' % text, viewcode_list)))
    # zip them up to get a tuple for each chip and join the fields
    if len(texts_list) > 0:
        annotation_text_list = [', '.join(tup) for tup in zip(*texts_list)]
    else:
        # no texts were specified return empty string for each input
        annotation_text_list = [''] * len(aid_list)
    return annotation_text_list
Пример #35
0
def get_all_descendant_rowids(depc, tablename, root_rowids, config=None,
                              ensure=True, eager=True, nInput=None,
                              recompute=False, recompute_all=False,
                              levels_up=None, _debug=False):
    r"""
    Connects `root_rowids` to rowids in `tablename`, and computes all
    values needed along the way. This is the main workhorse function for
    dependency computations.

    Args:
        tablename (str): table to compute dependencies to
        root_rowids (list): rowids for ``tablename``
        config (dict): config applicable for all tables (default = None)
        ensure (bool): eager evaluation if True(default = True)
        eager (bool): (default = True)
        nInput (None): (default = None)
        recompute (bool): (default = False)
        recompute_all (bool): (default = False)
        levels_up (int): only partially compute dependencies (default = 0)
        _debug (bool): (default = False)

    CommandLine:
        python -m dtool.depcache_control --exec-get_all_descendant_rowids:0
        python -m dtool.depcache_control --exec-get_all_descendant_rowids:1

    Example:
        >>> # DISABLE_DOCTEST
        >>> from dtool.depcache_control import *  # NOQA
        >>> from dtool.example_depcache import testdata_depc
        >>> depc = testdata_depc()
        >>> tablename = 'spam'
        >>> root_rowids = [1, 2]
        >>> config1 = {'dim_size': 500}
        >>> config2 = {'dim_size': 100}
        >>> config3 = {'dim_size': 500, 'adapt_shape': False}
        >>> ensure, eager, nInput = True, True, None
        >>> _debug = True
        >>> rowid_dict1 = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config1, ensure, eager, nInput, _debug=_debug)
        >>> rowid_dict2 = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config2, ensure, eager, nInput, _debug=_debug)
        >>> rowid_dict3 = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config3, ensure, eager, nInput, _debug=_debug)
        >>> result1 = 'rowid_dict1 = ' + ut.repr3(rowid_dict1, nl=1)
        >>> result2 = 'rowid_dict2 = ' + ut.repr3(rowid_dict2, nl=1)
        >>> result3 = 'rowid_dict3 = ' + ut.repr3(rowid_dict3, nl=1)
        >>> result = '\n'.join([result1, result2, result3])
        >>> print(result)
        rowid_dict1 = {
            'chip': [1, 2],
            'dummy_annot': [1, 2],
            'fgweight': [1, 2],
            'keypoint': [1, 2],
            'probchip': [1, 2],
            'spam': [1, 2],
        }
        rowid_dict2 = {
            'chip': [3, 4],
            'dummy_annot': [1, 2],
            'fgweight': [3, 4],
            'keypoint': [3, 4],
            'probchip': [1, 2],
            'spam': [3, 4],
        }
        rowid_dict3 = {
            'chip': [1, 2],
            'dummy_annot': [1, 2],
            'fgweight': [5, 6],
            'keypoint': [5, 6],
            'probchip': [1, 2],
            'spam': [5, 6],
        }


    Example:
        >>> # ENABLE_DOCTEST
        >>> from dtool.depcache_control import *  # NOQA
        >>> from dtool.example_depcache import testdata_depc
        >>> depc = testdata_depc()
        >>> _debug = True
        >>> tablename = 'vsmany'
        >>> config = depc.configclass_dict['vsmany']()
        >>> root_rowids = [1, 2, 3]
        >>> ensure, eager, nInput = False, True, None
        >>> # Get rowids of algo ( should be None )
        >>> rowid_dict = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config, ensure, eager, nInput,
        >>>     _debug=_debug)
        >>> result = ut.repr3(rowid_dict, nl=1)
        >>> print(result)
        {
            'dummy_annot': [1, 2, 3],
            'vsmany': [None, None, None],
        }

    Example:
        >>> # ENABLE_DOCTEST
        >>> from dtool.depcache_control import *  # NOQA
        >>> from dtool.example_depcache import testdata_depc
        >>> # Make sure algo config can correctly get properites
        >>> depc = testdata_depc()
        >>> tablename = 'chip'
        >>> recompute = False
        >>> recompute_all = False
        >>> _debug = True
        >>> root_rowids = [1, 2]
        >>> configclass = depc.configclass_dict['chip']
        >>> config_ = configclass()
        >>> config1 = depc.configclass_dict['vsmany'](dim_size=500)
        >>> config2 = depc.configclass_dict['vsmany'](dim_size=100)
        >>> config = config2
        >>> prop_dicts1 = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config=config1, _debug=_debug)
        >>> prop_dicts2 = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config=config2, _debug=_debug)
        >>> print(prop_dicts2)
        >>> print(prop_dicts1)
        >>> assert prop_dicts1 != prop_dicts2

    Example:
        >>> # ENABLE_DOCTEST
        >>> from dtool.depcache_control import *  # NOQA
        >>> from dtool.example_depcache import testdata_depc
        >>> depc = testdata_depc()
        >>> exec(ut.execstr_funckw(depc.get_all_descendant_rowids), globals())
        >>> _debug = True
        >>> qaids, daids = [1, 2, 4], [2, 3, 4]
        >>> root_rowids = list(zip(*ut.product(qaids, daids)))
        >>> request = depc.new_request('vsone', qaids, daids)
        >>> results = request.execute()
        >>> tablename = 'vsone'
        >>> rowid_dict = depc.get_all_descendant_rowids(
        >>>     tablename, root_rowids, config=None, _debug=_debug)
    """
    # TODO: Need to have a nice way of ensuring configs dont overlap
    # via namespaces.
    _debug = depc._debug if _debug is None else _debug
    indenter = ut.Indenter('[Descend-to-%s]' % (tablename,), enabled=_debug)
    if _debug:
        indenter.start()
        print(' * GET DESCENDANT ROWIDS %s ' % (tablename,))
        print(' * config = %r' % (config,))
    dependency_levels = depc.get_dependencies(tablename)
    if levels_up is not None:
        dependency_levels = dependency_levels[:-levels_up]

    configclass_levels = [
        [depc.configclass_dict.get(tablekey, None)
         for tablekey in keys]
        for keys in dependency_levels
    ]
    if _debug:
        print('[depc] dependency_levels = %s' %
              ut.repr3(dependency_levels, nl=1))
        print('[depc] config_levels = %s' %
              ut.repr3(configclass_levels, nl=1))

    # TODO: better support for multi-edges
    if (len(root_rowids) > 0 and ut.isiterable(root_rowids[0]) and
         not depc[tablename].ismulti):
        rowid_dict = {}
        for colx, col in enumerate(root_rowids):
            rowid_dict[depc.root + '%d' % (colx + 1,)] = col
        rowid_dict[depc.root] = ut.unique_ordered(ut.flatten(root_rowids))
    else:
        rowid_dict = {depc.root: root_rowids}

    # Ensure that each level ``tablename``'s dependencies have been computed
    for level_keys in dependency_levels[1:]:
        if _debug:
            print(' * level_keys %s ' % (level_keys,))
        # For each table in the level
        for tablekey in level_keys:
            try:
                child_rowids = depc._expand_level_rowids(
                    tablename, tablekey, rowid_dict, ensure, eager, nInput,
                    config, recompute, recompute_all, _debug)
            except Exception as ex:
                table = depc[tablekey]  # NOQA
                keys = ['tablename', 'tablekey', 'rowid_dict', 'config',
                        'table', 'dependency_levels']
                ut.printex(ex, 'error expanding rowids', keys=keys)
                raise
            rowid_dict[tablekey] = child_rowids
    if _debug:
        print(' GOT DESCENDANT ROWIDS')
        indenter.stop()
    return rowid_dict
Пример #36
0
def get_annot_texts(ibs, aid_list, **kwargs):
    """ Add each type of text_list to the strings list

    Args:
        ibs (IBEISController):  ibeis controller object
        aid_list (int):  list of annotation ids

    Returns:
        list: annotation_text_list

    CommandLine:
        python -m ibeis.viz.viz_helpers --test-get_annot_texts

    Example:
        >>> # ENABLE_DOCTEST
        >>> from ibeis.viz.viz_helpers import *  # NOQA
        >>> import ibeis
        >>> import collections
        >>> ibs = ibeis.opendb('testdb1')
        >>> # Default all kwargs to true
        >>> class KwargsProxy(object):
        ...    def get(self, a, b):
        ...        return True
        >>> kwargs_proxy = KwargsProxy()
        >>> aid_list = ibs.get_valid_aids()[::3]
        >>> # execute function
        >>> annotation_text_list = get_annot_texts(ibs, aid_list, kwargs_proxy=kwargs_proxy)
        >>> # verify results
        >>> result = ut.list_str(annotation_text_list)
        >>> print(result)
        [
            'aid1, gname=easy1.JPG, name=____, nid=-1, , nGt=0, quality=UNKNOWN, yaw=None',
            'aid4, gname=hard1.JPG, name=____, nid=-4, , nGt=0, quality=UNKNOWN, yaw=None',
            'aid7, gname=jeff.png, name=jeff, nid=3, EX, nGt=0, quality=UNKNOWN, yaw=None',
            'aid10, gname=occl2.JPG, name=occl, nid=5, EX, nGt=0, quality=UNKNOWN, yaw=None',
            'aid13, gname=zebra.jpg, name=zebra, nid=7, EX, nGt=0, quality=UNKNOWN, yaw=None',
        ]
    """
    # HACK FOR TEST
    if 'kwargs_proxy' in kwargs:
        kwargs = kwargs['kwargs_proxy']
    try:
        ibsfuncs.assert_valid_aids(ibs, aid_list)
        assert ut.isiterable(aid_list), 'input must be iterable'
        assert all([isinstance(aid, ut.VALID_INT_TYPES) for aid in aid_list]), 'invalid input'
    except AssertionError as ex:
        ut.printex(ex, 'invalid input', 'viz', key_list=['aid_list'])
        raise
    texts_list = []  # list of lists of texts
    if kwargs.get('show_aidstr', True):
        aidstr_list = get_aidstrs(aid_list)
        texts_list.append(aidstr_list)
    if kwargs.get('show_gname', False):
        gname_list = ibs.get_annot_image_names(aid_list)
        texts_list.append(['gname=%s' % gname for gname in gname_list])
    if kwargs.get('show_name', True):
        name_list = ibs.get_annot_names(aid_list)
        texts_list.append(['name=%s' % name for name in name_list])
    if kwargs.get('show_nid', False):
        nid_list = ibs.get_annot_name_rowids(aid_list)
        texts_list.append(['nid=%d' % nid for nid in nid_list])
    if kwargs.get('show_exemplar', True):
        flag_list = ibs.get_annot_exemplar_flags(aid_list)
        texts_list.append(['EX' if flag else '' for flag in flag_list])
    if kwargs.get('show_num_gt', True):
        # FIXME: This should be num_groundtruth with respect to the currently
        # allowed annotations
        nGt_list = ibs.get_annot_num_groundtruth(aid_list)
        texts_list.append(['nGt=%r' % nGt for nGt in nGt_list])
    if kwargs.get('show_quality_text', False):
        qualtext_list = ibs.get_annot_quality_texts(aid_list)
        texts_list.append(list(map(lambda text: 'quality=%s' % text, qualtext_list)))
    if kwargs.get('show_yawtext', False):
        # FIXME: This should be num_groundtruth with respect to the currently
        # allowed annotations
        yawtext_list = ibs.get_annot_yaw_texts(aid_list)
        texts_list.append(list(map(lambda text: 'yaw=%s' % text, yawtext_list)))
    # zip them up to get a tuple for each chip and join the fields
    if len(texts_list) > 0:
        annotation_text_list = [', '.join(tup) for tup in zip(*texts_list)]
    else:
        # no texts were specified return empty string for each input
        annotation_text_list = [''] * len(aid_list)
    return annotation_text_list
Пример #37
0
    def parse_column_tuples(
        self,
        col_name_list,
        col_types_dict,
        col_getter_dict,
        col_bgrole_dict,
        col_ider_dict,
        col_setter_dict,
        editable_colnames,
        sortby,
        sort_reverse=True,
        strict=False,
        **kwargs,
    ):
        """
        parses simple lists into information suitable for making guitool headers
        """
        # Unpack the column tuples into names, getters, and types
        if not strict:
            # slopply colname definitions
            flag_list = [colname in col_getter_dict for colname in col_name_list]
            if not all(flag_list):
                invalid_colnames = ut.compress(col_name_list, ut.not_list(flag_list))
                logger.info(
                    '[api_item_widget] Warning: colnames=%r have no getters'
                    % (invalid_colnames,)
                )
                col_name_list = ut.compress(col_name_list, flag_list)
            # sloppy type inference
            for colname in col_name_list:
                getter_ = col_getter_dict[colname]
                if colname not in col_types_dict:
                    type_ = ut.get_homogenous_list_type(getter_)
                    if type_ is not None:
                        col_types_dict[colname] = type_
        # sloppy kwargs.
        # FIXME: explicitly list col_nice_dict
        col_nice_dict = kwargs.get('col_nice_dict', {})
        self.col_nice_list = [col_nice_dict.get(name, name) for name in col_name_list]

        self.col_name_list = col_name_list
        self.col_type_list = [
            col_types_dict.get(colname, str) for colname in col_name_list
        ]
        # First col is always a getter
        self.col_getter_list = [
            col_getter_dict.get(colname, str) for colname in col_name_list
        ]
        # Get number of rows / columns
        self.nCols = len(self.col_getter_list)
        if self.nCols == 0:
            self.nRows = 0
        else:
            for getter in self.col_getter_list:
                if ut.isiterable(getter):
                    break
                getter = None
            # FIXME
            assert getter is not None, 'at least one getter must be an array/list'
            self.nRows = len(getter)

        # self.nRows = 0 if self.nCols == 0 else len(self.col_getter_list[0])  # FIXME
        # Init iders to default and then overwite based on dict inputs
        self.col_ider_list = [None] * self.nCols  # ut.alloc_nones(self.nCols)
        # for colname, ider_colnames in six.iteritems(col_ider_dict):
        # import utool
        # utool.embed()
        colname2_colx = ut.make_index_lookup(self.col_name_list)
        for colname, ider_colnames in six.iteritems(col_ider_dict):
            if colname not in colname2_colx:
                continue
            # for colname in self.col_name_list:
            ider_colnames = col_ider_dict[colname]
            try:
                colx = colname2_colx[colname]
                # Col iders might have tuple input
                ider_cols = self._uinput_1to1(self.col_name_list.index, ider_colnames)
                col_ider = self._uinput_1to1(lambda c: ut.partial(self.get, c), ider_cols)
                self.col_ider_list[colx] = col_ider
                del col_ider
                del ider_cols
                del colx
                del colname
            except Exception as ex:
                ut.printex(
                    ex,
                    keys=['colname', 'ider_colnames', 'colx', 'col_ider', 'ider_cols'],
                )
                raise
        # Init setters to data, and then overwrite based on dict inputs
        self.col_setter_list = list(self.col_getter_list)
        for colname, col_setter in six.iteritems(col_setter_dict):
            colx = colname2_colx[colname]
            self.col_setter_list[colx] = col_setter
        # Init bgrole_getters to None, and then overwrite based on dict inputs
        self.col_bgrole_getter_list = [
            col_bgrole_dict.get(colname, None) for colname in self.col_name_list
        ]
        # Mark edtiable columns
        self.col_edit_list = [name in editable_colnames for name in col_name_list]
        # Mark the sort column index
        if sortby is None:
            self.col_sort_index = 0
        elif ut.is_str(sortby):
            self.col_sort_index = self.col_name_list.index(sortby)
        else:
            self.col_sort_index = sortby
        self.col_sort_reverse = sort_reverse

        # Hacks for tree widget
        self._iders = kwargs.get('iders', None)
        col_level_dict = kwargs.get('col_level_dict', None)
        if col_level_dict is None:
            self.col_level_list = None
        else:
            self.col_level_list = ut.take(col_level_dict, col_name_list)
Пример #38
0
def get_aidstrs(aid_list, **kwargs):
    if ut.isiterable(aid_list):
        return [ibsfuncs.aidstr(aid, **kwargs) for aid in aid_list]
    else:
        return ibsfuncs.aidstr(aid_list, **kwargs)
Пример #39
0
def get_nidstrs(nid_list, **kwargs):
    if ut.isiterable(nid_list):
        return ['nid%d' for nid in nid_list]
    else:
        return 'nid%d' % nid_list
Пример #40
0
def get_nidstrs(nid_list, **kwargs):
    if ut.isiterable(nid_list):
        return ['nid%d' for nid in nid_list]
    else:
        return 'nid%d' % nid_list
Пример #41
0
def get_table_rows(cur, tablename, colnames, where=None, params=None, unpack=True):
    import utool as ut
    want_single_column = isinstance(colnames, six.string_types)
    want_single_param = params is not None and not ut.isiterable(params)
    #isinstance(params, six.string_types)
    if want_single_column:
        colnames = (colnames,)
    if colnames is not None and colnames != '*':
        assert isinstance(colnames, tuple), 'colnames must be a tuple'
        colnames_str = ', '.join(colnames)
    else:
        colnames_str = '*'
    #if isinstance(colnames, six.string_types):
    #    colnames = (colnames,)
    fmtdict = {
        'tablename'     : tablename,
        'colnames'    : colnames_str,
        'orderby'     : '',
    }

    #ORDER BY rowid ASC
    if where is None:
        operation_fmt = '''
        SELECT {colnames}
        FROM {tablename}
        {orderby}
        '''
    else:
        fmtdict['where_clause'] = where
        operation_fmt = '''
        SELECT {colnames}
        FROM {tablename}
        WHERE {where_clause}
        {orderby}
        '''
    operation_str = operation_fmt.format(**fmtdict)
    if params is None:
        cur.execute(operation_str)
        val_list = cur.fetchall()
    elif want_single_param:
        cur.execute(operation_str, (params,))
        val_list = cur.fetchall()
    else:
        # Execute many
        def executemany_scalar_generator(operation_str, params):
            for param in params:
                cur.execute(operation_str, param)
                vals = cur.fetchall()
                #assert len(vals) == 1, 'vals=%r, len(vals)=%r' % (vals, len(vals))
                yield vals
        val_list = list(executemany_scalar_generator(operation_str, params))

    if unpack:
        if want_single_column:
            # want a single value per parameter
            val_list = [val[0] for val in val_list]

        if want_single_param:
            # wants a single parameter
            assert len(val_list) == 1
            val_list = val_list[0]
    return val_list