示例#1
0
def collect_ibeis_training_annotations(ibs, nDaids_basis, verbose=True):
    # load a dataset
    #dbname = 'PZ_MTEST'
    #dbname = 'GZ_ALL'
    def get_set_groundfalse(ibs, qaids):
        # get groundfalse annots relative to the entire set
        valid_nids = ibs.get_valid_nids()
        qnids = ibs.get_annot_nids(qaids)
        nid_list = list(set(valid_nids) - set(qnids))
        aids_list = ibs.get_name_aids(nid_list)
        return ut.flatten(aids_list)

    # determanism
    np.random.seed(0)
    random.seed(0)
    # TODO: USE ANOT FILTERINGS
    import ibeis
    qaids_all = ibeis.testdata_aids(
        a='default:pername=1,mingt=2,is_known=True')
    qaids = qaids_all[::2]
    print('nQaids = %r' % len(qaids))

    def get_annot_groundtruth_sample(ibs,
                                     aid_list,
                                     per_name=1,
                                     isexemplar=True):
        r"""
        DEPRICATE
        """
        all_trues_list = ibs.get_annot_groundtruth(aid_list,
                                                   noself=True,
                                                   is_exemplar=isexemplar)

        def random_choice(aids):
            size = min(len(aids), per_name)
            return np.random.choice(aids, size, replace=False).tolist()

        sample_trues_list = [
            random_choice(aids) if len(aids) > 0 else []
            for aids in all_trues_list
        ]
        return sample_trues_list

    daids_gt_sample = ut.flatten(
        ibs.get_annot_groundtruth_sample(ibs, qaids, isexemplar=None))
    daids_gf_all = get_set_groundfalse(ibs, qaids)
    ut.assert_eq(len(daids_gt_sample), len(qaids), 'missing gt')
    daids_list = []

    for target_nDaids in ut.ProgressIter(nDaids_basis, lbl='testing dbsize'):
        print('---------------------------')
        # Sample one match from the groundtruth with padding
        daids_gf_sample = ut.random_sample(
            daids_gf_all, max(0, target_nDaids - len(daids_gt_sample)))
        daids = sorted(daids_gt_sample + daids_gf_sample)
        nDaids = len(daids)
        if target_nDaids != nDaids:
            continue
        daids_list.append(daids)
    return qaids, daids_list
示例#2
0
def collect_ibeis_training_annotations(ibs, nDaids_basis, verbose=True):
    # load a dataset
    #dbname = 'PZ_MTEST'
    #dbname = 'GZ_ALL'
    def get_set_groundfalse(ibs, qaids):
        # get groundfalse annots relative to the entire set
        valid_nids = ibs.get_valid_nids()
        qnids = ibs.get_annot_nids(qaids)
        nid_list = list(set(valid_nids) - set(qnids))
        aids_list = ibs.get_name_aids(nid_list)
        return ut.flatten(aids_list)

    # determanism
    np.random.seed(0)
    random.seed(0)
    # TODO: USE ANOT FILTERINGS
    import ibeis
    qaids_all = ibeis.testdata_aids(a='default:pername=1,mingt=2,is_known=True')
    qaids = qaids_all[::2]
    print('nQaids = %r' % len(qaids))

    def get_annot_groundtruth_sample(ibs, aid_list, per_name=1, isexemplar=True):
        r"""
        DEPRICATE
        """
        all_trues_list = ibs.get_annot_groundtruth(aid_list, noself=True, is_exemplar=isexemplar)
        def random_choice(aids):
            size = min(len(aids), per_name)
            return np.random.choice(aids, size, replace=False).tolist()
        sample_trues_list = [random_choice(aids) if len(aids) > 0 else [] for aids in all_trues_list]
        return sample_trues_list

    daids_gt_sample = ut.flatten(ibs.get_annot_groundtruth_sample(ibs, qaids, isexemplar=None))
    daids_gf_all = get_set_groundfalse(ibs, qaids)
    ut.assert_eq(len(daids_gt_sample), len(qaids), 'missing gt')
    daids_list = []

    for target_nDaids in ut.ProgressIter(nDaids_basis, lbl='testing dbsize'):
        print('---------------------------')
        # Sample one match from the groundtruth with padding
        daids_gf_sample = ut.random_sample(daids_gf_all, max(0, target_nDaids - len(daids_gt_sample)))
        daids = sorted(daids_gt_sample + daids_gf_sample)
        nDaids = len(daids)
        if target_nDaids != nDaids:
            continue
        daids_list.append(daids)
    return qaids, daids_list
示例#3
0
def test_avl_split(verbose=1):
    for num in range(0, 20):
        for index in range(num):
            if verbose:
                print('------')
                print('num = %r' % (num,))
                print('index = %r' % (index,))
            try:
                tree0 = EulerTourTree(ut.chr_range(num))
                tour = list(tree0)
                tree0._assert_nodes()
                if verbose >= 2:
                    tree0.print_tree()
                if verbose:
                    print('tree0 = %r' % (tree0,))
                node = tree0.get_node(index)
                if verbose:
                    print('node = %s' % (node,))
                part1, part2, bnode = avl_split(tree0.root, node)
                tree1 = EulerTourTree(root=part1)
                tree2 = EulerTourTree(root=part2)
                if verbose >= 2:
                    tree1.print_tree(), tree2.print_tree()
                if verbose:
                    print('tree1 = %r' % (tree1,))
                    print('tree2 = %r' % (tree2,))
                # Should correspond to a split in the tour list
                assert bnode.left is None, 'bnode must be split'
                assert bnode.right is None, 'bnode must be split'
                assert bnode.parent is None, 'bnode must be split'
                assert bnode is node, 'node must be same'
                ut.assert_eq(list(tree1), tour[:index])
                ut.assert_eq(list(tree2), tour[index + 1:])
                tree1._assert_nodes(), tree2._assert_nodes()
            except Exception:
                print('num = %r' % (num,))
                print('index = %r' % (index,))
                raise
示例#4
0
def get_toy_annots(num_annots,
                   num_names=None,
                   initial_aids=None,
                   initial_nids=None,
                   nid_sequence=None,
                   seed=None):
    r"""
    Args:
        num_annots (int):
        num_names (int): (default = None)
        initial_aids (None): (default = None)
        initial_nids (None): (default = None)
        nid_sequence (None): (default = None)
        seed (None): (default = None)

    Returns:
        tuple: (aids, nids, aids1, nids1, all_aids, all_nids)

    CommandLine:
        python -m ibeis.algo.hots.demobayes --exec-get_toy_annots

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.algo.hots.demobayes import *  # NOQA
        >>> num_annots = 1
        >>> num_names = 5
        >>> initial_aids = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)
        >>> initial_nids = np.array([0, 0, 1, 2, 2, 1, 1, 1, 2, 3], dtype=np.int64)
        >>> nid_sequence = np.array([0, 0, 1, 2, 2, 1, 1], dtype=np.int64)
        >>> seed = 0
        >>> (aids, nids, aids1, nids1, all_aids, all_nids) = get_toy_annots(num_annots, num_names, initial_aids, initial_nids, nid_sequence, seed)
        >>> result = ('(aids, nids, aids1, nids1, all_aids, all_nids) = %s' % (ut.repr2((aids, nids, aids1, nids1, all_aids, all_nids), nl=1),))
        >>> print(result)
    """
    import vtool as vt
    if num_names is None:
        num_names = num_annots
    print('Generating toy data with num_annots=%r' % (num_annots, ))
    if initial_aids is None:
        assert initial_nids is None
        first_step = True
        initial_aids = []
        initial_nids = []
    else:
        first_step = False
        assert initial_nids is not None

    aids = np.arange(len(initial_aids), num_annots + len(initial_aids))
    rng = vt.ensure_rng(seed)
    if nid_sequence is None:
        nids = rng.randint(0, num_names, num_annots)
    else:
        unused_from_sequence = max(len(nid_sequence) - len(initial_aids), 0)
        if unused_from_sequence == 0:
            nids = rng.randint(0, num_names, num_annots)
        elif unused_from_sequence > 0 and unused_from_sequence < num_annots:
            num_remain = num_annots - unused_from_sequence
            nids = np.append(nid_sequence[-unused_from_sequence:],
                             rng.randint(0, num_names, num_remain))
        else:
            nids = nid_sequence[-unused_from_sequence]
            nids = np.array(
                ut.take(
                    nid_sequence,
                    range(len(initial_aids),
                          len(initial_aids) + num_annots)))

    if first_step:
        aids1 = aids
        nids1 = nids
    else:
        aids1 = initial_aids
        nids1 = initial_nids

    all_nids = np.append(initial_nids, nids)
    all_aids = np.append(initial_aids, aids)
    import utool
    with utool.embed_on_exception_context:
        ut.assert_eq(len(aids), len(nids), 'len new')
        ut.assert_eq(len(aids1), len(nids1), 'len comp')
        ut.assert_eq(len(all_aids), len(all_nids), 'len all')
    return aids, nids, aids1, nids1, all_aids, all_nids
示例#5
0
def get_toy_annots(num_annots, num_names=None, initial_aids=None, initial_nids=None, nid_sequence=None, seed=None):
    r"""
    Args:
        num_annots (int):
        num_names (int): (default = None)
        initial_aids (None): (default = None)
        initial_nids (None): (default = None)
        nid_sequence (None): (default = None)
        seed (None): (default = None)

    Returns:
        tuple: (aids, nids, aids1, nids1, all_aids, all_nids)

    CommandLine:
        python -m ibeis.algo.hots.demobayes --exec-get_toy_annots

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.algo.hots.demobayes import *  # NOQA
        >>> num_annots = 1
        >>> num_names = 5
        >>> initial_aids = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)
        >>> initial_nids = np.array([0, 0, 1, 2, 2, 1, 1, 1, 2, 3], dtype=np.int64)
        >>> nid_sequence = np.array([0, 0, 1, 2, 2, 1, 1], dtype=np.int64)
        >>> seed = 0
        >>> (aids, nids, aids1, nids1, all_aids, all_nids) = get_toy_annots(num_annots, num_names, initial_aids, initial_nids, nid_sequence, seed)
        >>> result = ('(aids, nids, aids1, nids1, all_aids, all_nids) = %s' % (ut.repr2((aids, nids, aids1, nids1, all_aids, all_nids), nl=1),))
        >>> print(result)
    """
    import vtool as vt
    if num_names is None:
        num_names = num_annots
    print('Generating toy data with num_annots=%r' % (num_annots,))
    if initial_aids is None:
        assert initial_nids is None
        first_step = True
        initial_aids = []
        initial_nids = []
    else:
        first_step = False
        assert initial_nids is not None

    aids = np.arange(len(initial_aids), num_annots + len(initial_aids))
    rng = vt.ensure_rng(seed)
    if nid_sequence is None:
        nids = rng.randint(0, num_names, num_annots)
    else:
        unused_from_sequence = max(len(nid_sequence) - len(initial_aids), 0)
        if unused_from_sequence == 0:
            nids = rng.randint(0, num_names, num_annots)
        elif unused_from_sequence > 0 and unused_from_sequence < num_annots:
            num_remain = num_annots - unused_from_sequence
            nids = np.append(nid_sequence[-unused_from_sequence:], rng.randint(0, num_names, num_remain))
        else:
            nids = nid_sequence[-unused_from_sequence]
            nids = np.array(ut.take(nid_sequence, range(len(initial_aids), len(initial_aids) + num_annots)))

    if first_step:
        aids1 = aids
        nids1 = nids
    else:
        aids1 = initial_aids
        nids1 = initial_nids

    all_nids = np.append(initial_nids, nids)
    all_aids = np.append(initial_aids, aids)
    import utool
    with utool.embed_on_exception_context:
        ut.assert_eq(len(aids), len(nids), 'len new')
        ut.assert_eq(len(aids1), len(nids1), 'len comp')
        ut.assert_eq(len(all_aids), len(all_nids), 'len all')
    return aids, nids, aids1, nids1, all_aids, all_nids