示例#1
0
def fit_dml(**optional):
    #set parameters
    if 'Templates' in optional:
        path = optional['path']
    else:
        path = AD1.set_path()
        path += '/Templates_regular'
    if 'data_X' in optional and 'data_Y' in optional:  #data given
        X = optional['data_X']
        Y = optional['data_Y']
    else:  #read in data
        list_bats, _ = AD1.set_batscolor(Templates=path)  #bat species
        num_bats, num_total = AD1.set_numbats(
            list_bats, Templates=path)  #number of each bat species
        num_bats_dml, num_total_dml = AD1.set_numbats(list_bats,
                                                      Templates=path +
                                                      '/Templates_dml')
        #read normal templates
        regions, rectangles = AD1.read_templates(Templates=path)
        #read dml_templates
        regions2, rectangles2 = AD1.read_templates(Templates=path +
                                                   '/Templates_dml')
        #set variables
        templates = regions.copy()
        #combine both rectangles and regions
        for k in range(num_total_dml):
            rectangles[num_total + k] = rectangles2[k]
            regions[num_total + k] = regions2[k]

        #calculate features
        features = AD3.calc_features2(rectangles, regions, templates,
                                      list_bats, num_total)
        X = np.transpose(features)
        Y = np.zeros((X.shape[0], ), dtype=np.uint8)
        #Fill in Y matrix
        count = 0
        #go through the templates
        for i in range(len(list_bats)):  #i corresponds to a bat species
            for j in range(num_bats[i]):  #number of this type of bat
                Y[count] = i
                count += 1
        #same thing, but for templates_dml
        for i in range(len(list_bats)):  #i corresponds to a bat species
            for j in range(num_bats_dml[i]):  #number of this type of bat
                Y[count] = i
                count += 1
    #fit model
    model = dml.dmlmj.DMLMJ()
    model.fit(X, Y)
    D = model.transformer()
    #Export D-matrix
    if 'export' in optional:
        path = AD1.set_path()
        np.save(path + '/' + optional['export'] + '.npy', D)
    return (D)
def loading_init(**optional):
    regions_temp, rectangles_temp = AD1.read_templates(**optional)
    list_bats, colors_bat = AD1.set_batscolor(**optional)
    num_bats, num_total = AD1.set_numbats(list_bats, **optional)
    freq_bats, freq_range_bats, freq_peakT_bats, freq_peakF_bats = AD1.set_batfreq(
        rectangles_temp, regions_temp, list_bats, num_bats)
    return (freq_bats, freq_range_bats, freq_peakT_bats, freq_peakF_bats,
            list_bats, colors_bat, num_bats, num_total, regions_temp,
            rectangles_temp)
示例#3
0
def evaluation_SOM(**optional):
    #set parameters
    if 'path' in optional:
        path = optional['path']
    else:
        path = AD1.set_path()
    if 'dim1' in optional and 'dim2' in optional:
        network_dim = (optional['dim1'], optional['dim2'])
    else:
        para = AD1.set_parameters()
        network_dim = para[9]

    #calculate num_bats
    list_bats, _ = AD1.set_batscolor(Templates=path +
                                     '/Templates_regular')  #bat species
    num_bats, num_total = AD1.set_numbats(
        list_bats,
        Templates=path + '/Templates_regular')  #number of each bat species
    num_bats_dml, num_total_dml = AD1.set_numbats(list_bats,
                                                  Templates=path +
                                                  '/Templates_dml')
    num_bats_eval, num_total_eval = AD1.set_numbats(list_bats,
                                                    Templates=path +
                                                    '/Templates_eval')

    #read normal templates
    regions, rectangles = AD1.read_templates(Templates=path +
                                             '/Templates_regular')
    #read dml_templates
    regions2, rectangles2 = AD1.read_templates(Templates=path +
                                               '/Templates_dml')
    #read eval_templates
    regions3, rectangles3 = AD1.read_templates(Templates=path +
                                               '/Templates_eval')
    #set templates
    if 'templates_features' in optional:
        templates, _ = AD1.read_templates(
            Templates=optional['templates_features'] + '/Templates_regular')
    else:
        templates = regions.copy()
    #combine rectangles and regions
    for k in range(num_total_dml):
        rectangles[num_total + k] = rectangles2[k]
        regions[num_total + k] = regions2[k]

    print('Initialisation complete (1/3)')

    #calculate features for DML
    X = AD3.calc_features2(rectangles, regions, templates, list_bats,
                           num_total)
    Y = np.zeros((X.shape[1], ), dtype=np.uint8)
    #Fill in Y matrix
    count = 0
    #go through the templates
    for i in range(len(list_bats)):  #i corresponds to a bat species
        for j in range(num_bats[i]):  #number of this type of bat
            Y[count] = i
            count += 1
    #same thing, but for templates_dml
    for i in range(len(list_bats)):  #i corresponds to a bat species
        for j in range(num_bats_dml[i]):  #number of this type of bat
            Y[count] = i
            count += 1

    #calculate dml
    if 'DML' in optional:
        D = optional['dml']
    else:
        D = AD4.fit_dml(data_X=np.transpose(X), data_Y=Y)

    print('DML complete (2/3)')

    #add the final part to the data (eval)
    for k in range(num_total_eval):
        rectangles[num_total + num_total_dml + k] = rectangles3[k]
        regions[num_total + num_total_dml + k] = regions3[k]

    #final data
    X_final = AD3.calc_features2(rectangles, regions, templates, list_bats,
                                 num_total)
    Y_final = np.zeros((X_final.shape[1], ), dtype=np.uint8)
    #Fill in Y matrix
    Y_final[0:count] = Y  #we already have this data
    #add the eval templates
    for i in range(len(list_bats)):  #i corresponds to a bat species
        for j in range(num_bats_eval[i]):  #number of this type of bat
            Y_final[count] = i
            count += 1

    #make a SOM
    if 'SOM' in optional:
        net = optional['SOM']
    else:
        if 'Full' in optional and optional[
                'Full']:  #if Full_flag is present and set to true
            list_files = 0
            net, raw_data = AD4.fit_SOM(list_files,
                                        full=optional['full'],
                                        dim1=network_dim[0],
                                        dim2=network_dim[1],
                                        DML=D)
        elif 'List_files' in optional:  #fit on list of files
            Full_flag = False
            net, raw_data = AD4.fit_SOM(optional['List_files'],
                                        full=Full_flag,
                                        dim1=network_dim[0],
                                        dim2=network_dim[1],
                                        DML=D)
        else:  #fit on templates only
            list_files = 0  #is overwritten by features
            Full_flag = False  #overwritten by features
            if 'fit_eval' in optional and optional[
                    'eval_SOM']:  #present and true, use eval data to fit SOM
                #use X_final
                net, raw_data = AD4.fit_SOM(list_files,
                                            full=Full_flag,
                                            dim1=network_dim[0],
                                            dim2=network_dim[1],
                                            DML=D,
                                            features=X_final)
            else:  #exclude eval data to fit SOM
                #use X
                net, raw_data = AD4.fit_SOM(list_files,
                                            full=Full_flag,
                                            dim1=network_dim[0],
                                            dim2=network_dim[1],
                                            DML=D,
                                            features=X)

    print('SOM complete (3/3)')

    #make a plot
    if 'Plot_Flag' in optional:
        if optional['Plot_Flag']:  #if set to true
            m = X_final.shape[0]

            f, ax1 = plt.subplots()

            count = np.zeros((len(list_bats), ), dtype=np.uint8)
            col_list = [
                'ro', 'g>', 'b*', 'cv', 'm^', 'kd', 'r<', 'g1', 'b2', 'c3',
                'm4', 'k8', 'rs', 'gp', 'ch', 'm,', 'ro', 'g>', 'b*', 'cv',
                'm^', 'kd', 'r<', 'g1', 'b2', 'c3', 'm4', 'k8', 'rs', 'gp',
                'ch', 'm,', 'ro', 'g>', 'b*', 'cv', 'm^', 'kd', 'r<', 'g1',
                'b2', 'c3', 'm4', 'k8', 'rs', 'gp', 'ch', 'm,', 'ro', 'g>',
                'b*', 'cv', 'm^', 'kd', 'r<', 'g1', 'b2', 'c3', 'm4', 'k8',
                'rs', 'gp', 'ch', 'm,'
            ]
            for i in range(X_final.shape[1]):
                for j in range(len(list_bats)):
                    #take the datapoint and the bmu
                    t = X_final[:, i].reshape(np.array([m, 1]))
                    bmu, bmu_idx = AD4.find_bmu(t, net, m, D)
                    if Y_final[i] == j:  #class j
                        col = col_list[j]
                        if count[j] == 0:  #first hit
                            ax1.plot(bmu_idx[0],
                                     bmu_idx[1],
                                     col,
                                     label=list_bats[j])
                            count[j] += 1  #only do this once
                        else:  #plot without label
                            ax1.plot(bmu_idx[0], bmu_idx[1], col)
            #legend
            handles, labels = ax1.get_legend_handles_labels()
            plt.legend(handles, labels)
            plt.xlabel('Index 1 SOM')
            plt.ylabel('Index 2 SOM')
            #optional
            if 'title' in optional:
                plt.title(optional['title'])
            if 'export' in optional:
                plt.savefig(optional['export'])
            plt.show()
            plt.close()
    return (X_final, Y_final, net, D)