Ejemplo n.º 1
0
def test():
    import warnings
    warnings.simplefilter("ignore")
    ###########################################################################
    ############################### Neighs_Info ###############################

    ### Creation of possible combinations
#    pos_format_set_info = [None, 'integer', 'list', 'list_only', 'list_list',
#                           'list_tuple', 'list_tuple1', 'list_tuple2',
#                           'array', 'slice', 'tuple', 'tuple_int',
#                           'tuple_slice', 'tuple_tuple', 'tuple_others']

    ############################# USEFUL FUNCTIONS ############################
    joinpos = lambda x, y: x

    ### Creation of possible inputs
    creator_lvl = lambda lvl: tuple(np.random.randint(1, 10, int(lvl)))
    creator2_lvl = lambda sh: tuple(list(sh) + [np.random.randint(5)])

    extend_list = lambda lista, n: [lista for i in range(n)]
    extend_array = lambda array, n: np.array([array for i in range(n)])

    neighs0 = lambda: np.random.randint(100)
    sp_rel_pos0 = lambda: np.random.random()

    def create_neighs(sh, type_):
        neighs = neighs0()
        for i in range(len(sh)):
            if type_ == 'array':
                neighs = extend_array(neighs, sh[len(sh)-i-1])
            else:
                neighs = extend_list(neighs, sh[len(sh)-i-1])
#        if type(neighs) == int:
#            if type_ == 'array':
#                neighs = np.array([neighs])
#            else:
#                neighs = [neighs]
        return neighs

    def create_sp_rel_pos(sh, type_):
        sp_rel_pos = np.array([sp_rel_pos0()
                               for i in range(np.random.randint(1, 4))])
        for i in range(len(sh)):
            if type_ == 'array':
                sp_rel_pos = extend_array(sp_rel_pos, sh[len(sh)-i-1])
            else:
                sp_rel_pos = extend_list(sp_rel_pos, sh[len(sh)-i-1])
        if len(sh) == 0:
            if type_ == 'list':
                sp_rel_pos = list(sp_rel_pos)
        return sp_rel_pos

#    neighs_int = lambda: np.random.randint(100)
#    neighs_list = lambda x: list([neighs_int() for i in range(x)])
#    neighs_array = lambda x: np.array([neighs_int() for i in range(x)])
    neighs_slice = lambda top: slice(0, top)

    def creation_neighs_nfo(p, sh, k_len, nei_len):
        ## Presetting
        sh_static = sh
        if len(sh_static) == 3:
            sh_static = list(sh_static)
            sh_static[0] = k_len
            sh_static = tuple(sh_static)
#        if p[0] is True:
#            basic_sh = [] if len(sh) == 0 else [1]
#            sh_static = tuple(basic_sh+[sh[i] for i in range(1, len(sh))])
        # Neighs creation
        if p[6] == 'slice':
            neighs = neighs_slice(nei_len)
        else:
            # Use type_neighs and level
            neighs = create_neighs(sh_static, p[6])
        # Sp_rel_pos
        sp_rel_pos = create_sp_rel_pos(sh_static, p[7])
        if p[7] == 'list':
#            print type(sp_rel_pos), p, sh
            assert(type(sp_rel_pos) == list)

        ## Create structure p[4], p[6], p[7] None
        # Create structure
        if p[4] == 'raw':
            neighs_nfo = neighs
        elif p[4] == 'tuple':
            neighs_nfo = (neighs, np.arange(k_len))
        elif p[4] == 'tuple_only':
            neighs_nfo = (neighs, sp_rel_pos)
        elif p[4] == 'tuple_tuple':
            neighs_nfo = ((neighs, sp_rel_pos), np.arange(k_len))
        elif p[4] == 'list_tuple_only':
            neighs_nfo = [(neighs, sp_rel_pos) for i in range(k_len)]
        elif p[4] == 'tuple_list_tuple':
            neighs_nfo = ([(neighs, sp_rel_pos) for i in range(k_len)],
                          range(k_len))
        else:
            neighs_nfo = neighs
        return neighs_nfo

    ###########################################################################

    pos_ifdistance = [True, False, None]
    pos_constant_neighs = [True, False, None]
    pos_format_get_k_info = [None, "general", "default", "list", "integer"]
    pos_format_get_info = [None, "default", "general"]
    pos_type_neighs = [None, 'general', 'array', 'list', 'slice']
    pos_type_sp_rel_pos = [None, 'general', 'array', 'list']
    pos_format_level = [None, 0, 1, 2, 3]
    pos_format_structure = [None, 'raw', 'tuple', 'tuple_only', 'tuple_tuple',
                            'list_tuple_only', 'tuple_list_tuple']
    pos_staticneighs = [None, True, False]

    pos = [pos_constant_neighs, pos_ifdistance, pos_format_get_info,
           pos_format_get_k_info, pos_format_structure, pos_format_level,
           pos_type_neighs, pos_type_sp_rel_pos, pos_staticneighs]

    ###############################
    ###############################
    level_dependent = ['list_tuple_only', 'tuple_list_tuple']

    ### Testing possible combinations
    k = 0
    for p in product(*pos):
        ## General instantiation. It has to be able to eat any input
        neighs_info_general = Neighs_Info()
        ## Defintiion of forbidden combinations
        bool_error = p[4] in level_dependent and p[5] != 2
#        bool_error = bool_error or p[2] == "default" or p[3] == "default"
        ## Testing raising errors of forbidden combinations:
        if bool_error:
            try:
                boolean = False
                neighs_info = Neighs_Info(constant_neighs=p[0],
                                          ifdistance=p[1],
                                          format_get_info=p[2],
                                          format_get_k_info=p[3],
                                          format_structure=p[4],
                                          format_level=p[5],
                                          type_neighs=p[6],
                                          type_sp_rel_pos=p[7],
                                          staticneighs=p[8])
                boolean = True
                raise Exception("It has to halt here.")
            except:
                if boolean:
                    raise Exception("It has to halt here.")
            continue
        ## Avoid non-allowed
        if p[2] == "default" or p[3] == "default":
            continue
        if p[4] == 'list_tuple_only' and p[0]:
            continue
        tupletypes = ['tuple', 'tuple_only', 'tuple_tuple', 'list_tuple_only',
                      'tuple_list_tuple']
        if p[6] == 'slice' and p[4] in tupletypes:
            continue
        if not p[8] and p[6] == 'slice':
            continue
        ## TESTING:
        if p[3] == 'integer':
            continue
#        print p
        ## Save effort
        if p[4] == 'tuple':
            if np.random.random() < 0.9:
                continue

        ## Instantiation
        neighs_info = Neighs_Info(constant_neighs=p[0], ifdistance=p[1],
                                  format_get_info=p[2], format_get_k_info=p[3],
                                  format_structure=p[4], format_level=p[5],
                                  type_neighs=p[6], type_sp_rel_pos=p[7],
                                  staticneighs=p[8])
        neighs_info.set_information(100, 100)

        lvl = np.random.randint(4) if p[5] is None else p[5]
        sh = creator_lvl(lvl)
        iss_len = sh[len(sh)-2] if len(sh) > 1 else np.random.randint(1, 100)
        k_len = sh[0] if len(sh) == 3 else np.random.randint(1, 9)
        nei_len = sh[-1] if len(sh) > 0 else np.random.randint(100)
        neighs_nfo = creation_neighs_nfo(p, sh, k_len, nei_len)

        neighs_info.set(neighs_nfo, range(iss_len))
        ks = [0] if neighs_info.ks is None else neighs_info.ks
        neighs_info.get_information(ks)
        ## Special cases
        # Slice type
        if p[6] == 'slice' and p[4] == 'raw':
            neighs_info.set(5, range(iss_len))
            neighs_info.set((2, 6), range(iss_len))
            neighs_info.set(None, range(iss_len))

        # Important general functions
        neighs_info.any()
        neighs_info.empty()
        neighs_info.shape
        neighs_info._get_neighs_general()
        neighs_info._general_get_rel_pos()
        neighs_info.get_copy_iss([neighs_info.iss[0]])
        neighs_info.get_copy_iss(neighs_info.iss[0])
        neighs_info.get_copy_iss_by_ind([0])
        neighs_info.get_copy_iss_by_ind(0)
        neighs_info.reset_functions()
        if p[6] == 'slice':
            neighs_info._get_neighs_general()
        # Reset structure
        if p[4] not in ['list_tuple_only', 'tuple_list_tuple']:
            # Rewrite level problems avoiding
            neighs_info.reset_level(p[5])
            neighs_info.reset_structure(p[4])
        neighs_info_general.set(neighs_nfo, range(iss_len))

        ## Testing joining
        logi = neighs_info.ifdistance and neighs_info.sp_relative_pos is None
        if np.random.random() < 0.1:
            if logi or type(neighs_info.idxs) == slice:
                try:
                    boolean = False
                    mode = ['and', 'or', 'xor'][np.random.randint(0, 3)]
                    neighs_info.join_neighs(neighs_info, mode, joinpos)
                    boolean = True
                    raise Exception("It has to halt here.")
                except:
                    if boolean:
                        raise Exception("It has to halt here.")
            else:
                neighs_info.join_neighs(neighs_info, 'and', joinpos)
                neighs_info.join_neighs(neighs_info, 'or', joinpos)
                neighs_info.join_neighs(neighs_info, 'xor', joinpos)
                join_neighsinfo_AND_general(neighs_info, neighs_info, joinpos)
                join_neighsinfo_OR_general(neighs_info, neighs_info, joinpos)
                join_neighsinfo_XOR_general(neighs_info, neighs_info, joinpos)
                neighs_nfo2 = creation_neighs_nfo(p, sh, k_len, nei_len)
                neighs_info2 = neighs_info.copy()
                neighs_info2.set(neighs_nfo2, range(iss_len))
                neighs_info2.sp_relative_pos = neighs_info.sp_relative_pos
                neighs_info.join_neighs(neighs_info2, 'and', joinpos)
                neighs_info.join_neighs(neighs_info2, 'or', joinpos)
                neighs_info.join_neighs(neighs_info2, 'xor', joinpos)
                join_neighsinfo_AND_general(neighs_info, neighs_info2, joinpos)
                join_neighsinfo_OR_general(neighs_info, neighs_info2, joinpos)
                join_neighsinfo_XOR_general(neighs_info, neighs_info2, joinpos)
        ## Set ks
        if neighs_info.staticneighs:
            neighs_info.set_ks(range(10))
        else:
            neighs_info.set_ks(range(neighs_info.shape[2]))

        ## Testing joinning
        if p[6] != 'slice':
            neis = [neighs_info.copy() for s in range(5)]
            ilen = 0
            for s in range(5):
                ilen += len(neis[s].iss)
            new_neis = join_by_iss(neis)
            assert(len(new_neis.iss) == ilen)
            assert(neighs_info.staticneighs == new_neis.staticneighs)
            assert(neighs_info.ks == new_neis.ks)
        join_by_iss([neighs_info])

        k += 1
#        print '-'*20, k

    #* integer {neighs}
    neighs_info = Neighs_Info()
    neighs_info.shape
    try:
        boolean = False
        neighs_info._default_get_neighs()
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        boolean = False
        neighs_info._default_get_information()
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")

    neighs_info._format_set_iss('int')
    neighs_info._format_set_iss('list')
    neighs_info._format_set_iss('null')
    neighs_info = Neighs_Info()
    neighs_info.set_information(10, 10)
    neighs_info.set(5)
    neighs_info.set(([0], [5.]))
    #* list of integers {neighs}
    neighs_info.reset()
    neighs_info.set_information(10, 10)
    neighs_info.set([[0, 4]])
    try:
        boolean = False
        neighs_info._kret = 10
        neighs_info._integer_get_k(100000)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    neighs_info._get_neighs_list_static()
    #* list of lists of integers {neighs for some iss}
    neighs_info.reset()
    neighs_info.set([[0, 4], [0, 3]])
    #* list of lists of lists of integers {neighs for some iss and ks}
    neighs_info.reset()
    neighs_info.set([[[0, 4], [0, 3]]])
    neighs_info.staticneighs = False
    neighs_info._array_only_set_rel_pos(np.array([[2], [3]]))
    neighs_info._general_set_iss(True)
    neighs_info._set_rel_pos_general_array(np.array([[2], [3]]))
    neighs_info._set_rel_pos_general_array(np.array([[[[2], [3]]]]))
    neighs_info._list_list_only_set_rel_pos(np.array([[[5]]]))
    neighs_info.staticneighs = True
    neighs_info._general_set_iss(True)
    neighs_info._set_rel_pos_general_list([[[[2], [3]]]])
    neighs_info._general_set_rel_pos(None)

    try:
        boolean = False
        neighs_info._general_set_rel_pos(True)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    #* numpy array 1d, 2d, 3d {neighs}
    neighs_info = Neighs_Info()
    neighs_info._set_neighs_general_array(np.array(5))
    neighs_info._set_neighs_slice(None)
    neighs_info.reset()

    try:
        boolean = False
        neighs_info._general_set_neighs(True)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")

    neighs_info.reset()
    neighs_info.set(np.array([[[0, 4], [0, 3]]]))
    neighs_info.staticneighs = False
    neighs_info._set_rel_pos_dim(5)
    neighs_info.reset()
    neighs_info.set(np.array([[0, 4], [0, 3]]))
    neighs_info.reset()
    neighs_info.set(np.array([0, 4]))
    #* tuple of neighs

    # Empty cases
    neighs_info.reset()
    neighs_info.set([[]])
    assert(neighs_info.empty())
    neighs_info = Neighs_Info(staticneighs=True)
    neighs_info.set([[]])

    neighs_info.reset()
    try:
        boolean = False
        neighs_info._set_tuple_only_structure((range(2), None, 5))
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    neighs_info._set_structure_list([np.array(range(10))])
    neighs_info.staticneighs = True
    # Testing setting rel_pos strange cases
    neighs_info._general_set_rel_pos(5)
    neighs_info._set_rel_pos_general_list(np.array([0]))
    neighs_info._set_rel_pos_general_list(np.array([[0]]))
    neighs_info._set_rel_pos_general_list(np.array([]))
    neighs_info._set_rel_pos_general_list(np.array([[]]))
    neighs_info._set_rel_pos_general_list(np.array([[[]]]))
    neighs_info._set_rel_pos_number(5)
    neighs_info.level = 0
    neighs_info._set_rel_pos_general_list(np.array([0, 3]))

    ## Get k
    neighs_info._kret = 10
    neighs_info._integer_get_k([4])
    neighs_info._integer_get_k(5)
    neighs_info._default_get_k()
    try:
        boolean = False
        neighs_info._integer_get_k(100000)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    neighs_info._general_get_k()
    neighs_info._general_get_k(4)
    neighs_info._general_get_k(range(2))

    ## Set iss
    neighs_info._general_set_iss(np.array([0]))
    neighs_info._general_set_iss(np.array([0]))
    neighs_info._general_set_iss(0)
    neighs_info._general_set_iss([0])
    neighs_info._general_set_iss(range(3))
    neighs_info._general_set_iss(5)
    neighs_info._int_set_iss(8)
    neighs_info._list_set_iss(range(3))
    neighs_info._null_set_iss(5)

    ## Stress empty tests
    neighs_info.reset()
    neighs_info.set(([[]], [[]]))
    assert(neighs_info.empty())

    neighs_info.reset()
    neighs_info.set(([[], []]))
    assert(neighs_info.empty())

    neighs_info.reset()
    neighs_info.set([])
    assert(neighs_info.empty())

    neighs_info.reset()
    neighs_info.set(([]))
    assert(neighs_info.empty())

    neighs_info.reset()
    neighs_info.set(([[]], [[]]), [0])
    assert(neighs_info.empty())

    neighs_info.reset()
    neighs_info.set(([np.array([])], [np.array([])]), [0])
    assert(neighs_info.empty())

    neighs_info = Neighs_Info(staticneighs=True)
    neighs_info._set_neighs_general_array(np.array(5))

    neighs_info = Neighs_Info(format_structure='tuple_tuple',
                              type_neighs='list', type_sp_rel_pos='list',
                              format_level=2)
    neighs_info.set((([np.array([])], [np.array([[]])]), [0]))
    neighs, _, _, _ = neighs_info.get_information(0)

    ## Empty assert
    neighs_info = Neighs_Info()
    neighs_info.set((([0], [0]), 0))
    assert(not neighs_info.empty())
    for a, b, c, d in neighs_info:
        pass

    ## Check proper outputs
    # Neighs information
    neighs_info = Neighs_Info()
    sh = creator_lvl(3)
    neighs = np.random.randint(0, 20, np.prod(sh)).reshape(sh)
    sp_relative_pos = np.random.random((tuple(list(sh)+[3])))
    ks = range(sh[0])
    iss = range(sh[1])

    neighs_info.set((neighs, sp_relative_pos), iss)
    neighs_info.check_output_standards(neighs, sp_relative_pos, ks, iss)

    neighs_info2 = Neighs_Info()
    neighs_info2.set(neighs_info)

    # Check wrong cases
    try:
        boolean = False
        sh1 = list(sh)
        sh1[0] = sh[0]+1
        sh1 = tuple(sh1)
        neighs = np.random.randint(0, 20, np.prod(sh1)).reshape(sh1)
        sp_relative_pos = np.random.random((tuple(list(sh1)+[3])))
        neighs_info.check_output_standards(neighs, sp_relative_pos, ks, iss)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        boolean = False
        sh1 = list(sh)
        sh1[1] = sh[1]+1
        sh1 = tuple(sh1)
        neighs = np.random.randint(0, 20, np.prod(sh1)).reshape(sh1)
        sp_relative_pos = np.random.random((tuple(list(sh1)+[3])))
        neighs_info.check_output_standards(neighs, sp_relative_pos, ks, iss)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        boolean = False
        neighs_info.check_output_standards(None, sp_relative_pos, ks, iss)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        boolean = False
        neighs_info.idxs = None
        neighs_info.assert_goodness()
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
# Why should be halt?
#    try:
#        boolean = False
#        neighs_info.idxs = [[]]
#        neighs_info.staticneighs = True
#        neighs_info.assert_goodness()
#        boolean = True
#        raise Exception("It has to halt here.")
#    except:
#        if boolean:
#            raise Exception("It has to halt here.")

    ## Testing auxiliar functions
    ###############################

    neis = np.random.randint(0, 100, 4*5).reshape((5, 2, 2))
    pars = inspect_raw_neighs(neis, range(5))
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set((neis, range(5)))
    assert(len(neighs_info.idxs) == 5)
    assert(len(neighs_info.idxs[0]) == 2)
    assert(len(neighs_info.ks) == 5)
    pars = inspect_raw_neighs(neis[0], 4)
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set((neis[0], 4))
    assert(neighs_info.staticneighs)
    assert(len(neighs_info.idxs) == 2)
    assert(len(neighs_info.ks) == 1)
    pars = inspect_raw_neighs(range(10), 2)
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set((range(10), 2))
    assert(neighs_info.staticneighs)
    assert(len(neighs_info.idxs) == 1)
    assert(len(neighs_info.idxs[0]) == 10)
    assert(len(neighs_info.ks) == 1)
    pars = inspect_raw_neighs(0, range(2))
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set((0, range(2)))
    assert(neighs_info.staticneighs)
    assert(len(neighs_info.idxs) == 1)
    assert(len(neighs_info.ks) == 2)
    pars = inspect_raw_neighs([], 2)
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set(([], 2))
    assert(neighs_info.staticneighs)
    assert(len(neighs_info.idxs) == 1)
    assert(len(neighs_info.idxs[0]) == 0)
    assert(len(neighs_info.ks) == 1)
    pars = inspect_raw_neighs([[]], 2)
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set(([[]], 2))
    assert(neighs_info.staticneighs)
    assert(len(neighs_info.idxs) == 1)
    assert(len(neighs_info.idxs[0]) == 0)
    assert(len(neighs_info.ks) == 1)
    pars = inspect_raw_neighs([[[]]]*2, range(2))
    pars['format_structure'] = 'tuple_k'
    neighs_info = Neighs_Info(**pars)
    neighs_info.set(([[[]]]*2, range(2)))
    assert(len(neighs_info.idxs) == 2)
    assert(len(neighs_info.idxs[0]) == 1)
    assert(len(neighs_info.idxs[0][0]) == 0)
    assert(len(neighs_info.ks) == 2)

    neighsinfo_features_preformatting_tuple((neis, range(5)), 8)
    neighsinfo_features_preformatting_tuple((neis[0], 4), 8)
    neighsinfo_features_preformatting_tuple((range(10), 2), 8)
    neighsinfo_features_preformatting_tuple((0, range(2)), 8)
    neighsinfo_features_preformatting_tuple(([], 2), 8)
    neighsinfo_features_preformatting_tuple(([[]], 2), 8)
    neighsinfo_features_preformatting_tuple(([[[]]]*2, range(2)), 8)
def test():
    ###########################################################################
    #### Testing auxiliar resulters
    ## Functions which helps to manage descriptors and build result measure
    ##
    def test_resulter_functions(fm):
        ## Application of joiner functions
        creation_concatenator_joiner()
        creation_null_joiner()
        ## Creation function tests
        default_creation_initializations(fm)
        if all([e is not None for e in fm.shape_measure]):
            creation_initialization_output_closearray(fm)
        creation_initialization_output_list(fm)
        creation_initialization_output_lists(fm)
        creation_initialization_output_list_selfdriven(fm)
        creation_initialization_desc_dict(fm)
        creation_initialization_desc_array(fm)
        ## Creation of resulters tests
        resulter = DefaultResulter(fm)
        GeneralResulter(*resulter.get_functions())

    ## Definition parameters
    n = 1000
    rei = 10

    n, n_feats = np.random.randint(10, 1000), np.random.randint(1, 20)
    ks = np.random.randint(1, 20)

    ###########################################################################
    ##########################
    #### FeatureRetriever testing
    reindices0 = np.arange(n)
    reindices = np.vstack([reindices0]+[np.random.permutation(n)
                                        for i in range(rei-1)]).T
    perturbation = PermutationPerturbation(reindices)

    aggcatfeats_dict = categorical_agg_dict_features(n, n_feats, ks)
    ## Impossible instantiation cases
    try:
        # Not valid oject as a feature
        boolean = False
        fm = FeaturesManager(None, None)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        # Object not valid as a feature
        boolean = False
        avgdesc = AvgDescriptor()
        fm = FeaturesManager([], avgdesc)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
#    try:
#        boolean = False
#        Feat_imp = ImplicitFeatures(contfeats_ar0, perturbation)
#        fm = FeaturesManager(Feat_imp, None)
#        boolean = True
#        raise Exception("It has to halt here.")
#    except:
#        if boolean:
#            raise Exception("It has to halt here.")
    try:
        # Different k_perturb
        boolean = False
        feats0 = ExplicitFeatures(np.random.random((100, 2, 4)))
        feats1 = ExplicitFeatures(np.random.random((100, 3, 3)))
        fm = FeaturesManager([feats0, feats1])
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        # Object not valid as a features
        boolean = False
        fm = FeaturesManager([5])
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        # Object not valid as a features
        boolean = False
        fm = FeaturesManager(lambda x: x)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")

    feats0 = np.random.random(100)
    feats1 = np.random.random((100, 1))
    feats2 = np.random.random((100, 1, 1))
    feats3 = np.random.random((100, 2))
    Feat_imp = ImplicitFeatures(feats1)
    Feat_imp2 = ImplicitFeatures(feats3, names=[3, 4])
    Feat_exp = ExplicitFeatures(aggcatfeats_dict)
    avgdesc = AvgDescriptor()

    pos_feats = [feats0, feats1, feats2, Feat_imp, Feat_exp,
                 [feats2, Feat_imp]]
    pos_mapvals_i = [None, ('matrix', 100, 20)]#, lambda x: x, 'matrix']
    pos_map_in = [None, lambda i_info, k: i_info]
    pos_map_out = [None, lambda self, feats: feats]
    pos_mode = [None, 'parallel', 'sequential']
    pos_desc = [None, avgdesc]

    possibilities = [pos_feats, pos_map_in, pos_map_out, pos_mapvals_i,
                     pos_mode, pos_desc]

    ## Random parameter space exploration
    mapper0 = [None]*3
    mapper1 = [(0, 0)]*3
    mapper2 = [np.array([np.zeros(100), np.zeros(100)]).T]*3
    mapper3 = [lambda idx: (0, 0)]*3
    mapper4 = [(0, 0), (0, 0), (1, 0)]
    pos_mappers = [mapper0, mapper1, mapper2, mapper3, mapper4]

    ## Information of indices
    nei_i = Neighs_Info()
#    nei_i.set(np.random.randint(0, 100, 5).reshape((5, 1, 1)))
    nei_i.set(np.random.randint(0, 100))
    nei_info = Neighs_Info()
    nei_info.set(np.random.randint(0, 100, 20).reshape((5, 2, 2)))

    ## Combinations
    for p in product(*possibilities):
#        ## Random exploration of parameters
#        feats = pos_feats[np.random.randint(0, len(pos_feats))]
#        m_input = pos_map_in[np.random.randint(0, len(pos_map_in))]
#        m_out = pos_map_out[np.random.randint(0, len(pos_map_out))]
#        m_vals_i = pos_mapvals_i[np.random.randint(0, len(pos_mapvals_i))]
#        mode = pos_mode[np.random.randint(0, len(pos_mode))]
#        desc = pos_desc[np.random.randint(0, len(pos_desc))]
        ## Exhaustive exploration of parameters
        i_selector = np.random.randint(0, len(pos_mappers))
        selectors = pos_mappers[i_selector]
        if i_selector == 4:
            continue
        #print i_selector
        feats, m_input, m_out, m_vals_i, mode, desc = p
        ## Instantiation
        fm = FeaturesManager(feats, maps_input=m_input, maps_output=m_out,
                             maps_vals_i=m_vals_i, mode=mode,
                             descriptormodels=desc, selectors=selectors)
        ## Basic parameters
        i0, i1 = 0, range(4)
        k_p = fm.k_perturb+1
        nei_info0 = Neighs_Info()
        nei_info1 = Neighs_Info()
        neis = np.random.randint(0, 100, 8*k_p).reshape((k_p, 4, 2))
        neis0 = np.random.randint(0, 100, 2*k_p).reshape((k_p, 1, 2))
        nei_info0.set(neis0)
        nei_info1.set(neis)
        ## Check basic functions
        fm[0]
        fm.shape
        len(fm)
        fm.set_map_vals_i(m_vals_i)
        fm.initialization_desc()
        fm.initialization_output()
        fm.set_map_vals_i(100)
        fm.set_map_vals_i(m_vals_i)
        fm.set_descriptormodels(desc)
        ## Check basic functions
        fm.get_type_feats(0)
        fm.get_type_feats(50)
        fm.get_type_feats(0, tuple([(0, 0)]*3))
        fm.get_type_feats(50, tuple([(0, 0)]*3))
        # fm.get_type_feats(i0)
        # fm.get_type_feats(i1)

        t_feat_in, t_feat_out, t_feat_des = fm.get_type_feats(50)
        tf_in0, tf_out0, tf_desc0 = fm.get_type_feats(i0)
        tf_in1, tf_out1, tf_desc1 = fm.get_type_feats(i1)
        ## Interaction with featuresObjects
        # Input
        fm._get_input_features(50, k=range(k_p), typefeats=t_feat_in)
        if i_selector == 0:
            fm._get_input_features([50], k=range(k_p), typefeats=[(0, 0)])
        desc_i0 = fm._get_input_features(i0, k=range(k_p), typefeats=tf_in0)
        desc_i1 = fm._get_input_features(i1, k=range(k_p), typefeats=tf_in1)
#        print feats, len(desc_i0), k_p
#        print fm._get_input_features, desc_i0, desc_i1
        assert(len(desc_i0) == k_p)
        assert(len(desc_i1) == k_p)
        assert(len(desc_i0[0]) == 1)
        assert(len(desc_i1[0]) == 4)
        # Output
        fm._get_output_features(range(10), k=range(k_p), typefeats=t_feat_out)
        fm._get_output_features(neis[0], k=range(k_p), typefeats=t_feat_out)
        fm._get_output_features(neis, k=range(k_p), typefeats=t_feat_out)
        if i_selector == 0:
            fm._get_output_features([50], k=range(k_p), typefeats=[(0, 0)])
        desc_nei0 = fm._get_output_features(nei_info0, range(k_p), tf_out0)
        desc_nei1 = fm._get_output_features(nei_info1, range(k_p), tf_out1)
#        print fm._get_output_features
        assert(len(desc_nei0) == k_p)
        assert(len(desc_nei1) == k_p)
        assert(len(desc_nei0[0]) == 1)
        assert(len(desc_nei1[0]) == 4)
#        print desc_i0, desc_i1, desc_nei
#        print type(desc_i0), type(desc_i1), type(desc_nei)
#        print len(desc_i0), len(desc_i1), len(desc_nei)
        ## Interaction with map_vals_i
        fm._get_vals_i(20, range(k_p))
        fm._get_vals_i(range(20), range(k_p))
        vals_i0 = fm._get_vals_i(i0, range(k_p))
        vals_i1 = fm._get_vals_i(i1, range(k_p))
#        print fm._get_vals_i, vals_i0, vals_i1
        assert(len(vals_i0) == k_p)
        assert(len(vals_i1) == k_p)
        assert(len(vals_i0[0]) == 1)
        assert(len(vals_i1[0]) == 4)

        ## Completing features
        fm._complete_desc_i(i0, nei_info0, desc_i0, desc_nei0, vals_i0,
                            tf_desc0)
        fm._complete_desc_i(i1, nei_info1, desc_i1, desc_nei1, vals_i1,
                            tf_desc1)
        fm._complete_desc_i(i0, nei_info0, desc_i0, desc_nei0, vals_i0,
                            (1, 0))
        fm._complete_desc_i(i1, nei_info1, desc_i1, desc_nei1, vals_i1,
                            (1, 0))

        ## Computing altogether
        fm.compute_descriptors(i0, nei_info0)
        fm.compute_descriptors(i1, nei_info1)
        fm.compute_descriptors(i0, nei_info0, range(k_p))
        fm.compute_descriptors(i1, nei_info1, range(k_p))
#        fm.compute_descriptors(i0, range(10), range(k_p))
#        fm.compute_descriptors(i1, range(10), range(k_p))
        fm.compute_descriptors(i0, neis0[0], range(k_p))
        fm.compute_descriptors(i1, neis[0], range(k_p))
        fm.compute_descriptors(i0, neis0, range(k_p))
        fm.compute_descriptors(i1, neis, range(k_p))
        if i_selector == 0:
            fm.compute_descriptors([50], neis0[0], k=range(k_p),
                                   feat_selectors=[tuple([(0, 0)]*3)])
        # Strange cases
        if mode is None:
            FeaturesManager([ImplicitFeatures(feats1),
                             ImplicitFeatures(feats3, names=[3, 4])],
                            mode=mode)
        ## Test resulter functions
        test_resulter_functions(fm)

    ## Cases
    feats = [ImplicitFeatures(feats1), ImplicitFeatures(feats1)]
    fm = FeaturesManager(feats, maps_input=m_input, maps_output=m_out,
                         maps_vals_i=m_vals_i, mode=mode,
                         descriptormodels=desc, selectors=selectors)
    if all([fea.typefeat == 'implicit' for fea in fm.features]):
        fm.add_perturbations(perturbation)

    ## Impossible function cases
    feats = [ImplicitFeatures(feats1), ImplicitFeatures(feats3, names=[3, 4])]
    try:
        ## Different variablesnames for sequential mode
        boolean = False
        fm = FeaturesManager(feats, maps_input=m_input, maps_output=m_out,
                             maps_vals_i=m_vals_i, mode='sequential',
                             descriptormodels=desc, selectors=selectors)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")

    try:
        boolean = False
        fm[-1]
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")

    ###########################################################################
    #### Testing auxiliar parsing
    ## Functions which carry the uniformation of inputs from possible ways to
    ## input features information.
    ##
    feats0 = np.random.randint(0, 10, 100)
    feats1 = feats0.reshape((100, 1))
    feats2 = np.random.random((100, 2, 3))
    desc = DummyDescriptor()
    pars_feats = {}

    # Testing combinations of possible inputs
    feats_info = feats0
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = feats1
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = feats2
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = (feats0, pars_feats)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = (feats1, pars_feats)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = (feats2, pars_feats)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = (feats0, pars_feats, desc)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = (feats1, pars_feats, desc)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = (feats2, pars_feats, desc)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))

    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    features_ret = _features_parsing_creation(features_obj)
    assert(isinstance(features_ret, FeaturesManager))
    features_ret = _features_parsing_creation([features_obj])
    assert(isinstance(features_ret, FeaturesManager))
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    pars_feats = {}
    feats_info = (features_obj, pars_feats)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = ([features_obj], pars_feats)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = (features_obj, pars_feats, desc)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = (features_obj, pars_feats, [desc, desc])
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))

    feats_info = ((feats0, {}), pars_feats)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = ((feats0, {}), pars_feats, desc)
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = ((feats0, {}), pars_feats, [desc, desc])
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))
    pars_feats = {}
    feats_info = ((feats0, {}, desc), pars_feats, [desc, desc])
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))

    feats_info = features_ret
    features_ret = _features_parsing_creation(feats_info)
    assert(isinstance(features_ret, FeaturesManager))

    features_ret = _featuresmanager_parsing_creation(features_obj)
    assert(isinstance(features_ret, FeaturesManager))
    features_ret = _featuresmanager_parsing_creation(features_ret)
    assert(isinstance(features_ret, FeaturesManager))

    feats_info = ((feats0, {}, desc), {})
    features_ret = _featuresmanager_parsing_creation(features_ret)
    assert(isinstance(features_ret, FeaturesManager))
    feats_info = ((feats0, {}, desc), {}, [desc, desc])
    features_ret = _featuresmanager_parsing_creation(features_ret)
    assert(isinstance(features_ret, FeaturesManager))
 def test_getfeatsk(Feat):
     nei = Neighs_Info()
     nei.set((([0], [0]), [0]))
     i, d, _, k = 0
     pass
    def test_getitem(Feat):
        #k = 0
        #idxs = np.random.randint(0, 5, 20).reshape((1, 4, 5))
        #Feat._get_feats_k(idxs, k)
        #Feat._get_feats_k(list(idxs), k)
        #Feat[[]]
        feat = Feat[0]
        assert(len(feat[0]) == 1)
        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[(0, 0)]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
        feat = Feat[([0], [0])]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
#        INVALID
#        feat = Feat[([0], [0.])]
#        assert(len(feat[0]) == 1)
#        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[range(5), [0]]
        assert(len(feat[0]) == 5)
        assert(len(feat) == 1)
        feat = Feat[0:3]
        assert(len(feat[0]) == 3)
        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[:]
#        print len(feat[0]),  len(Feat)
        assert(len(feat[0]) == len(Feat))
        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[((0, 0), 0)]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
        feat = Feat[(([0], [0]), [0])]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
        Feat[[[[]]], [0]]
        if Feat.k_perturb:
#            print 'x'*100, Feat.k_perturb, Feat.shape
            Feat[(([[0], [0]], [[0], [0]]), [0, 1])]
        feat = Feat[[0, 4, 5]]
        assert(len(feat[0]) == 3)
        assert(len(feat) == Feat.k_perturb+1)
        try:
            boolean = False
            Feat[-1]
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat._retrieve_feats([[[0]]], -1, None)
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat._retrieve_feats([[[0]]], 10000, None)
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat[len(Feat)]
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat[range(4), range(Feat.k_perturb+3)]
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        nei = Neighs_Info()
        nei.set((([0], [0]), [0]))
        Feat[nei]
        nei = Neighs_Info()
        nei.set([[[0, 4], [0, 3]]])
        Feat[nei]
        nei = Neighs_Info(staticneighs=True)
        nei.set([[0, 4], [0, 3]])
        Feat[nei, 0]
        # shape
        Feat.shape
        ## Empty call
        Feat[(([[]], [[]]), [0])]
        # Descriptormodels setting
        # null formatters
        #Feat._format_characterizer(None, None)
        if Feat.typefeat != 'phantom':
            Feat.set_descriptormodel(dum1desc)
            if Feat.typefeat == 'implicit':
                Feat.set_descriptormodel(dum2desc)
            else:
                Feat.set_descriptormodel(dum2desc_agg)

            avgdesc = AvgDescriptor()
            Feat.set_descriptormodel(avgdesc)

        ## Export features
        feat_o, feat_info, feat_pars = Feat.export_features()
        new_feats = feat_o(feat_info, **feat_pars)
        assert(isinstance(new_feats, feat_o))
def test():
    ## Definition parameters
    n = 1000
    m = 5
    rei = 10

    n, n_feats = np.random.randint(10, 1000), np.random.randint(1, 20)
    n_feats2 = [np.random.randint(1, 20) for i in range(n_feats)]
    ks = np.random.randint(1, 20)

    def create_ids(n1):
        aux = np.random.randint(1, 4, n1)
        return np.cumsum(aux)

    def create_featurenames(n1):
        aux = create_ids(n1)
        return [str(e) for e in aux]

    def extract_featurenames_agg(aggdictfeats):
        names = []
        for k in range(len(aggdictfeats)):
            names += extract_featurenames(aggdictfeats[k])
        names = list(set(names))
        return names

    def extract_featurenames(aggdictfeats):
        names = []
        for i in range(len(aggdictfeats)):
            names += aggdictfeats[i].keys()
        names = list(set(names))
        return names

    def compute_featurenames(features):
        names = []
        if type(features) == np.ndarray:
            names = [str(e) for e in range(len(features[0]))]
        return names

    class DummyDesc:
        def set_functions(self, typefeatures, outformat):
            pass

    class Dummy1Desc(DummyDesc):
        def __init__(self):
            self.compute = lambda x, d: [e[0] for e in x]
            self._out_formatter = lambda x, y1, y2, y3: x
            self._f_default_names = compute_featurenames

    class Dummy2Desc_exp(DummyDesc):
        def __init__(self):
            self.compute = lambda x, d: [e[0] for e in x]
            self._out_formatter = lambda x, y1, y2, y3: x
            self._f_default_names = compute_featurenames

    class Dummy2Desc_imp(DummyDesc):
        def __init__(self):
            self.compute = lambda x, d: np.array([e[0] for e in x])
            self._out_formatter = lambda x, y1, y2, y3: x
            self._f_default_names = compute_featurenames

    ## Possible descriptormodels to test
    avgdesc = AvgDescriptor()
    dum1desc = Dummy1Desc()
    dum2desc = Dummy2Desc_imp()
    dum2desc_agg = Dummy2Desc_exp()

    ### Test functions definitions
    def test_getfeatsk(Feat):
        nei = Neighs_Info()
        nei.set((([0], [0]), [0]))
        i, d, _, k = 0
        pass

    def test_getitem(Feat):
        #k = 0
        #idxs = np.random.randint(0, 5, 20).reshape((1, 4, 5))
        #Feat._get_feats_k(idxs, k)
        #Feat._get_feats_k(list(idxs), k)
        #Feat[[]]
        feat = Feat[0]
        assert(len(feat[0]) == 1)
        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[(0, 0)]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
        feat = Feat[([0], [0])]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
#        INVALID
#        feat = Feat[([0], [0.])]
#        assert(len(feat[0]) == 1)
#        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[range(5), [0]]
        assert(len(feat[0]) == 5)
        assert(len(feat) == 1)
        feat = Feat[0:3]
        assert(len(feat[0]) == 3)
        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[:]
#        print len(feat[0]),  len(Feat)
        assert(len(feat[0]) == len(Feat))
        assert(len(feat) == Feat.k_perturb+1)
        feat = Feat[((0, 0), 0)]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
        feat = Feat[(([0], [0]), [0])]
        assert(len(feat[0]) == 1)
        assert(len(feat) == 1)
        Feat[[[[]]], [0]]
        if Feat.k_perturb:
#            print 'x'*100, Feat.k_perturb, Feat.shape
            Feat[(([[0], [0]], [[0], [0]]), [0, 1])]
        feat = Feat[[0, 4, 5]]
        assert(len(feat[0]) == 3)
        assert(len(feat) == Feat.k_perturb+1)
        try:
            boolean = False
            Feat[-1]
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat._retrieve_feats([[[0]]], -1, None)
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat._retrieve_feats([[[0]]], 10000, None)
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat[len(Feat)]
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        try:
            boolean = False
            Feat[range(4), range(Feat.k_perturb+3)]
            boolean = True
            raise Exception("It has to halt here.")
        except:
            if boolean:
                raise Exception("It has to halt here.")
        nei = Neighs_Info()
        nei.set((([0], [0]), [0]))
        Feat[nei]
        nei = Neighs_Info()
        nei.set([[[0, 4], [0, 3]]])
        Feat[nei]
        nei = Neighs_Info(staticneighs=True)
        nei.set([[0, 4], [0, 3]])
        Feat[nei, 0]
        # shape
        Feat.shape
        ## Empty call
        Feat[(([[]], [[]]), [0])]
        # Descriptormodels setting
        # null formatters
        #Feat._format_characterizer(None, None)
        if Feat.typefeat != 'phantom':
            Feat.set_descriptormodel(dum1desc)
            if Feat.typefeat == 'implicit':
                Feat.set_descriptormodel(dum2desc)
            else:
                Feat.set_descriptormodel(dum2desc_agg)

            avgdesc = AvgDescriptor()
            Feat.set_descriptormodel(avgdesc)

        ## Export features
        feat_o, feat_info, feat_pars = Feat.export_features()
        new_feats = feat_o(feat_info, **feat_pars)
        assert(isinstance(new_feats, feat_o))

    ## Definition arrays
    aggfeatures = np.random.random((n/2, m, rei))
    features0 = np.random.random((n, m))
    features1 = np.random.random((n, m))
    features2 = np.vstack([np.random.randint(0, 10, n) for i in range(m)]).T
    reindices0 = np.arange(n)
    reindices = np.vstack([reindices0]+[np.random.permutation(n)
                                        for i in range(rei-1)]).T
    perturbation = PermutationPerturbation(reindices)

    ###########################################################################
    ##########################
    #### Explicit Features testing
    ### Definition classes
    # Instantiation
    Feat = ExplicitFeatures(np.random.randint(0, 20, 100))
    test_getitem(Feat)
    Feat = ExplicitFeatures(np.random.random((100, 2)))
    test_getitem(Feat)
    Feat = ExplicitFeatures(aggfeatures)
    test_getitem(Feat)
    try:
        boolean = False
        ExplicitFeatures(np.random.random((10, 1, 1, 1)))
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It should not accept that inputs.")

    ## Exhaustive instantiation testing
    aggcontfeats_ar0 = continuous_agg_array_features(n, n_feats, ks)
    aggcatfeats_ar0 = categorical_agg_array_features(n, n_feats, ks)
    aggcatfeats_ar1 = categorical_agg_array_features(n, n_feats2, ks)
    aggcontfeats_dict = continuous_agg_dict_features(n, n_feats, ks)
    aggcatfeats_dict = categorical_agg_dict_features(n, n_feats, ks)

    pos_feats = [aggcontfeats_ar0, aggcatfeats_ar0, aggcatfeats_ar1,
                 aggcontfeats_dict, aggcatfeats_dict]
    pos_names = [create_featurenames(n_feats), create_featurenames(1),
                 create_featurenames(len(n_feats2)),
                 create_featurenames(n_feats),
                 extract_featurenames_agg(aggcontfeats_dict),
                 extract_featurenames_agg(aggcatfeats_dict)]
    pos_nss = [0, 1, 2, 3, 4]
    pos_null = [None, 0., np.inf]
    pos_characterizer = [None]
#    pos_outformatter = [None]
    pos_indices = [None]

    possibilities = [pos_nss, pos_null, pos_characterizer, pos_indices]

    for p in product(*possibilities):
#        print p
        ## Names definition
        names = []
        if np.random.randint(0, 2):
            names = pos_names[p[0]]
        ## Instantiation
        Feat = ExplicitFeatures(pos_feats[p[0]], descriptormodel=p[2],
                                names=names, indices=p[3], nullvalue=p[1])
        ## Testing main functions
        test_getitem(Feat)

    ## Particular cases
    try:
        boolean = False
        names = [str(i) for i in range(len(aggcontfeats_ar0[0])+1)]
        ExplicitFeatures(aggcontfeats_ar0, names=names, indices=p[3],
                         descriptormodel=p[2], nullvalue=p[1])
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    ##
    ## List features
    listfeatures = []
    for k in range(5):
        listfeatures_k = []
        for i in range(100):
            aux = np.unique(np.random.randint(0, 100, np.random.randint(5)))
            d = dict(zip(aux, np.random.random(len(aux))))
            listfeatures_k.append(d)
        listfeatures.append(listfeatures_k)
    Feat = ExplicitFeatures(listfeatures)
    len(Feat)
    nei = Neighs_Info()
    nei.set((([0], [0]), [0]))
    Feat[nei]
    nei = Neighs_Info()
    nei.set([[[0, 4], [0, 3]]])
    Feat[nei]

    ###########################################################################
    ##########################
    #### Implicit Features testing
    ### Definition classes
    # Instantiation
    contfeats_ar0 = continuous_array_features(n, n_feats)
    catfeats_ar0 = categorical_array_features(n, n_feats)
    catfeats_ar1 = categorical_array_features(n, n_feats2)
    contfeats_dict = continuous_dict_features(n, n_feats)
    catfeats_dict = categorical_dict_features(n, n_feats)

    pos_feats = [contfeats_ar0, catfeats_ar0, catfeats_ar1,
                 contfeats_dict, catfeats_dict]
    pos_names = [create_featurenames(n_feats), create_featurenames(1),
                 create_featurenames(len(n_feats2)),
                 create_featurenames(n_feats),
                 extract_featurenames(contfeats_dict),
                 extract_featurenames(catfeats_dict)]
    pos_nss = [0, 1, 2, 3, 4]
    pos_null = [None]  # TODO: [None, 0., np.inf]
    pos_characterizer = [None]
#    pos_outformatter = [None]
    pos_indices = [None]  # TODO
    pos_perturbations = [None, perturbation]

    possibilities = [pos_nss, pos_null, pos_characterizer, pos_indices,
                     pos_perturbations]
    ## Combination of inputs testing
    for p in product(*possibilities):
#        print p
        ## Names definition
        names = []
        if np.random.randint(0, 2):
            names = pos_names[p[0]]
        ## Instantiation
        Feat = ImplicitFeatures(pos_feats[p[0]], names=names,
                                descriptormodel=p[2], perturbations=p[4])
        ## Testing main functions
#        if p[0] < 3:
#            test_getitem(Feat)
        test_getitem(Feat)

    Feat_imp = ImplicitFeatures(contfeats_ar0, perturbations=perturbation)
    test_getitem(Feat_imp)
    Feat_imp = ImplicitFeatures(catfeats_ar0, perturbations=perturbation)
    test_getitem(Feat_imp)
    Feat_imp = ImplicitFeatures(catfeats_ar0.ravel(),
                                perturbations=perturbation, names=[0])
    test_getitem(Feat_imp)
    Feat_imp = ImplicitFeatures(catfeats_ar1, perturbations=perturbation)
    test_getitem(Feat_imp)
    Feat_imp = ImplicitFeatures(contfeats_dict, perturbations=perturbation)
#    test_getitem(Feat_imp)
    Feat_imp = ImplicitFeatures(catfeats_dict, perturbations=perturbation)
#    test_getitem(Feat_imp)

    try:
        boolean = False
        Feat = ImplicitFeatures(contfeats_ar0, perturbations=None)
        Feat._map_perturb(-1)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        boolean = False
        Feat = ImplicitFeatures(contfeats_ar0, perturbations=perturbation)
        Feat._map_perturb(-1)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")
    try:
        boolean = False
        Feat = ImplicitFeatures(contfeats_ar0, perturbations=perturbation)
        Feat._map_perturb(1000)
        boolean = True
        raise Exception("It has to halt here.")
    except:
        if boolean:
            raise Exception("It has to halt here.")

    ###########################################################################
    ##########################
    #### Phantom Features testing
    ### Definition classes
    # Instantiation
    pos_fea_info = [(100, 25)]
    pos_perturbations = [None, perturbation]
    pos_names = [[]]
    pos_outfeats = [[]]
    pos_characterizer = [None, ]
#    pos_outformatter = [None]

    possibilities = [pos_fea_info, pos_perturbations, pos_names, pos_outfeats,
                     pos_characterizer]
    ## Combination of inputs testing
    for p in product(*possibilities):
#        print p
        fe = PhantomFeatures(features_info=p[0], perturbations=p[1],
                             names=p[2], out_features=p[3],
                             descriptormodel=p[4])
        test_getitem(fe)

    ###########################################################################
    #### Testing auxiliar parsing
    feats0 = np.random.randint(0, 10, 100)
    feats1 = feats0.reshape((100, 1))
    feats2 = np.random.random((100, 2, 3))
    desc = DummyDescriptor()
    pars_feats = {}

    # Testing combinations of possible inputs
    feats_info = feats0
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = feats1
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = feats2
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = (feats0, pars_feats)
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = (feats1, pars_feats)
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = (feats2, pars_feats)
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = (feats0, pars_feats, desc)
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = (feats1, pars_feats, desc)
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    feats_info = (feats2, pars_feats, desc)
    features_obj = _featuresobject_parsing_creation(feats_info)
    assert(isinstance(features_obj, BaseFeatures))
    features_obj = _featuresobject_parsing_creation(features_obj)
    assert(isinstance(features_obj, BaseFeatures))