Exemple #1
0
    def test_clear_functions(self):
        print "TEST CLEAR FUNCTIONS\n"
        digraph = nx.DiGraph(self.simple_digraph)
        edges = [(1, 2, {
            'weight': 1
        }), (2, 3, {
            "weight": 1
        }), (3, 4, {
            "weight": 1
        })]
        digraph.add_edges_from(edges)
        for node in digraph.nodes():
            digraph.node[node]['value'] = 1

        n = {
            1: 5,
            2: 5,
            3: 5,
            4: 5,
        }
        myFCM = FCM.FCM(digraph, FCM.bivalent_pos_zero)
        myFCM.clear_edges()
        myFCM.clear_nodes()
        print myFCM.get_edges()
        print myFCM.get_nodes()
        myFCM.set_nodes(n)
        myFCM.set_edges(edges)
        print myFCM.get_edges()
        print myFCM.get_nodes()
Exemple #2
0
 def setUp(self):
     self.df = pd.read_csv("iris-setosa.csv")
     self.X = self.df.iloc[:, 1:5].values
     self.y = self.df.iloc[:, 0].values
     self.model = FCM()
     self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
         self.X, self.y)
Exemple #3
0
    def run_fcm(self):
        f = FCM.FCM(self.village_pixels)
        centroids = f.run()

        clusters = f.devidPixelsToClusters()

        for i in centroids:
            self.centroids.append(i)

        return self.centroids, clusters
 def rbf_train(self):
     np.random.shuffle(self.dataset)
     for i in range(int(len(self.dataset) * 0.7)):
         self.Y_matrix[i][self.dataset[i][1] - 1] = 1
     fcm = FCM.FCM(self.n_cluster, self.dataset[0:int(len(self.dataset) * 0.7)], self.m)
     self.U_matrix, self.centroid_matrix_ = fcm.clustering()
     self.compute_G(0, int(len(self.dataset) * 0.7), 0)
     self.W_matrix = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(self.G_matrix),
                                                                 self.G_matrix)), np.transpose(self.G_matrix)),
                               self.Y_matrix)
Exemple #5
0
def createInitialFCM(nodeDict, edgeDict):
    fcm = FCM.FCM()

    for key in nodeDict:
        fcm.add_concept(key)
        fcm.set_value(key, nodeDict[key])

    for key in edgeDict:
        ## Edit: Use first value in the range.
        fcm.add_edge(key[0], key[1], edgeDict[key[0], key[1]][0])
    return fcm
Exemple #6
0
def fcm_script(data):

    #Da utilizzare se so a priori i nomi delle colonne
    #feat1=sys.argv[2]
    #feat2=sys.argv[3]
    #labels=sys.argv[4]

    dataset = pd.read_csv(data)

    # extract features and labels
    #X = dataset[[feat1, feat2]].values
    #y = dataset[labels].values

    X = dataset.iloc[:, 1:]
    y = dataset.iloc[:, 0]
    X = np.asarray(X)
    y = np.asarray(y)

    model = FCM()

    N_SPLITS = 5
    N_CLUSTER = 2
    error = []
    score = []

    #cross validation
    skf = StratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=1)
    for train_index, test_index in skf.split(X, y):
        #print("TRAIN:", train_index, "\nTEST:", test_index)
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]

        #training
        train_membership, centers = model.fuzzy_train(X_train, N_CLUSTER, 2)

        #test
        test_membership = model.fuzzy_predict(X_test, N_CLUSTER, centers, 2)

        if (N_CLUSTER == 2):
            error.append(model.RMSE_membership(test_membership, y_test))

        else:
            error.append(model.RMSE(test_membership, y_test))

        score.append(model.accuracy(test_membership, y_test))

    return model, score, error
Exemple #7
0
class FCM_GA_TESTER(unittest.TestCase):
    #digraph, threshold_function, t_func_params = None, dep_coef=1, past_coef=0, normalize_initial_values = True
    myFCM = FCM.FCM(networkx.DiGraph(), FCM.no_func)
    genome = G2DList.G2DList(3, 3)
    genome.setParams(rangemin=0, rangemax=10)
    myGA = FCM_GA.LearnFCM(test_data, [1, 2, 3], myFCM,
                           genome)  #input_data, concept_list, myFCM, genome

    def test_error_initial_(self):
        '''testing the error_initial function'''
        result = self.myGA.error_initial(input_data1, input_data2, 3)
        self.assertEqual(result, 4)

    def test_GA(self):
        self.myGA.setGenerations(100)
        self.myGA.evolve(10)
        print(self.myGA.bestIndividual())
Exemple #8
0
def experiment1():
    #digraph, threshold_function, t_func_params = None, dep_coef=1, past_coef=0, normalize_initial_values = True
    myFCM = FCM.FCM(nx.DiGraph(), FCM.trivalent)

    node_list = [1, 2, 3, 4, 5, 6]
    initial_state = [1, 1, 1, 1, 1, 1]
    density_percent = .4
    valid_input = False
    seed = 0
    while not valid_input:
        seed += time.time()
        print "seed is: "
        print seed
        input_FCM = FCM.generate_random_FCM_matrix(node_list,
                                                   density_percent,
                                                   seed,
                                                   FCM.pos_neg_int,
                                                   allow_self_edge=True)
        input_data = myFCM.calculate_next_states_matrix(input_FCM,
                                                        initial_state,
                                                        n=30)
        print input_data
        if not myFCM.simple_pattern_recognition(input_data):
            valid_input = True
    print "\n\nINPUT FCM IS:"
    print input_FCM
    print "INPUT DATA IS:"
    for row in input_data:
        print row
    print "RUNNING GA TO ATTEMPT TO LEARN FCM"
    genome = G2DList.G2DList(len(node_list), len(node_list))
    genome.setParams(rangemin=-1, rangemax=1)
    myGA = FCM_GA.LearnFCM(input_data, node_list, myFCM, genome)
    myGA.setPopulationSize(1000)
    myGA.setGenerations(100)
    myGA.setMutationRate(0.3)
    myGA.setMinimax(Consts.minimaxType["minimize"])
    myGA.evolve(10)
    print myGA.bestIndividual()
    print "THE ERROR FOR THE BEST INDIVIDUAL IS:"
    print myGA.eval_func_matrix(myGA.bestIndividual())
    print "ACTUAL FCM"
    print input_FCM
 def Run_Rbf(self):
     np.random.shuffle(self.dataset)
     for i in range(int(len(self.dataset) * 0.7)):
         self.Y_matrix[i][self.dataset[i][1] - 1] = 1
     fcm = FCM.FCM(self.n_cluster,
                   self.dataset[0:int(len(self.dataset) * 0.7)],
                   self.m)  # ue FCM
     self.U_matrix, self.C_matrix = fcm.clustering_algorithm()
     self.compute_G(0, int(len(self.dataset) * 0.7), 0)
     self.W_matrix = np.matmul(
         np.matmul(
             np.linalg.inv(
                 np.matmul(np.transpose(self.G_matrix), self.G_matrix)),
             np.transpose(self.G_matrix)), self.Y_matrix)
     self.Output_matrix = np.matmul(self.G_matrix, self.W_matrix)
     print('W_matrix:')
     print(self.W_matrix)
     print('output:')
     print(self.Output_matrix)
Exemple #10
0
import sys
from FCM import *

def f1() : return 0.2

def f2(a=3) : return 0.4


fcm_graph=None

# Test 1 : Check for FCM creation
fcm_graph=FCM()

print '### FCM creation successful'

# Test 2 : Test for adding concepts
fcm_graph.add_concept("concept1")
fcm_graph.add_concept("concept2")

print '### FCM adding valid concepts successful'


# Test 3 : Adding valid edges

fcm_graph.add_edge('concept1','concept2',0.3)

print '### FCM adding valid edges successful'

fcm_graph.remove_edge('concept1','concept2')
print '### FCM removing valid edges successful'
Exemple #11
0
header = pd.read_csv(file_name, nrows=0).columns.tolist()
data = pd.read_csv(file_name)

if len(header) == 2:
    # Plot the data
    fig = plt.figure()
    f = fig.add_subplot()
    f.scatter(data[header[0]], data[header[1]], c='b')

max_num_clusters = 11
x = data.values
num_data = len(x)
steps = 50
minimum_error = 10000000
best_num_clusters = 0

# Defining answer
for c in range(2, max_num_clusters):
    fcm = FCM.FCM(num_data, c, header, steps, x)
    e, centers = fcm.run()
    print(e)
    if e < minimum_error:
        minimum_error = e
        best_num_clusters = c

if len(header) == 2:
    ce = 0
    for ce in range(best_num_clusters):
        f.scatter(centers[ce, 0], centers[ce, 1], c='red')
    plt.show()
Exemple #12
0
import FCM
from FCM import simulation
import math, time


def transFunct(x):
    return 1 / (1 + math.exp(-x))


#declare the fcm for test
fcm1 = FCM.FCM()

fcm1.add_concept("Tank1")
fcm1.add_concept("Tank2")
fcm1.add_concept("Valve1")
fcm1.add_concept("Valve2")
fcm1.add_concept("Valve3")
fcm1.add_concept("Heat element")
fcm1.add_concept("Therm_tank1")
fcm1.add_concept("Therm_tank2")

fcm1.set_value("Tank1", .2)
fcm1.set_value("Tank2", .01)

fcm1.set_value("Valve1", .55)
fcm1.set_value("Valve2", .58)
fcm1.set_value("Valve3", .0)

fcm1.set_value("Heat element", .2)
fcm1.set_value("Therm_tank1", .1)
fcm1.set_value("Therm_tank2", .05)
Exemple #13
0
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 18 15:26:05 2017

@author: CNLAB
"""
from FCM import *

a = FCM(dataSet, 2)

centroids = a.randCent(3).A

a.row, a.col = a.dataset.shape
u2, centroids = a.alg_Pfcm(centroids)
Exemple #14
0
import sys
from FCM import *
from Simulation import *

test_fcm1 = FCM()

test_fcm1.add_concept("Tank1")
test_fcm1.add_concept("Tank2")
test_fcm1.add_concept("Valve1")
test_fcm1.add_concept("Valve2")
test_fcm1.add_concept("Valve3")
test_fcm1.add_concept("Heat element")
test_fcm1.add_concept("Therm_tank1")
test_fcm1.set_value("Tank1", .2)
test_fcm1.add_concept("Therm_tank2")

test_fcm1.set_value("Tank2", .01)

test_fcm1.set_value("Valve1", .55)
test_fcm1.set_value("Valve2", .58)
test_fcm1.set_value("Valve3", .0)

test_fcm1.set_value("Heat element", .2)
test_fcm1.set_value("Therm_tank1", .1)
test_fcm1.set_value("Therm_tank2", .05)

test_fcm1.add_edge("Tank1", "Valve1", .21)
test_fcm1.add_edge("Tank1", "Valve2", .38)
test_fcm1.add_edge("Tank2", "Valve2", .7)
test_fcm1.add_edge("Tank2", "Valve3", .6)
test_fcm1.add_edge("Valve1", "Tank1", .76)
Exemple #15
0
    optimal_boundary = []

    for i in range(len(boundary)):
        #get the majority pixel count in the boundary
        majority_pixel_count = initial.getMajorityPixelsCount(
            boundary[i], non_village_pixels)

        #check if it is in the threshold

        if majority_pixel_count < threshold:
            optimal_boundary.append(boundary[i])
        else:
            #run FCM for particular cluster, if its majority pixel count is greater than the threshold
            if len(clusters[i]) > 5:
                f = FCM.FCM(clusters[i])
                centroids = f.run()
                clusters_updated = f.devidPixelsToClusters()

                #get the updated boundary
                updated_boundary_fcm = initial.newClusterBoundary(
                    clusters_updated, non_village_pixels, "1.png")

                for j in range(len(updated_boundary_fcm)):
                    update_majority_pixel_count = initial.getMajorityPixelsCount(
                        updated_boundary_fcm[j], non_village_pixels)

                    if update_majority_pixel_count < threshold:
                        optimal_boundary.append(updated_boundary_fcm[j])
                    else:
                        if len(clusters_updated[i]) > 5:
Created on Thu Oct 13 22:16:15 2016

@author: Eric
"""

#from FCM import FCM
#from FCM import simulation
import math
import FCM


def transFunct(x):
    return (1 / (1 + math.exp(-x)))


test_fcm = FCM.FCM()

test_fcm.add_concept("Tank1")
test_fcm.add_concept("Tank2")
test_fcm.add_concept("Valve1")
test_fcm.add_concept("Valve2")
test_fcm.add_concept("Valve3")
test_fcm.add_concept("Heat element")
test_fcm.add_concept("Therm_tank1")
test_fcm.add_concept("Therm_tank2")

test_fcm.set_value("Tank1", .2)
test_fcm.set_value("Tank2", .01)

test_fcm.set_value("Valve1", .55)
test_fcm.set_value("Valve2", .58)
def Spatial_CFIS(configFile, DataFolder):
    #ap = argparse.ArgumentParser()
    # ap.add_argument("-c", "--config", required = True, help = "path to config")
    # ap.add_argument("-d", "--data", required = True, help= "path to Data")
    # ap.add_argument("-o", "--output", required = True, help = "path to predict image")
    # ap.add_argument("-a", "--accuracy", required = True, help = "path to accuracy folder")
    # #ap.add_argument("-id","--userID", required = True, help = "User ID")
    # args = vars(ap.parse_args())
    try:
        with open(configFile, 'r') as f:
            data = f.read()
        Bs_data = Bs(data, "xml")
    except:
        print("Please choose *.xml or format config xml file same as tutorial")
    t = int(Bs_data.FCM.t.contents[0])
    c = int(Bs_data.FCM.c.contents[0])
    m = int(Bs_data.FCM.m.contents[0])
    eps = float(Bs_data.FCM.eps.contents[0])
    lr = float(Bs_data.Adam.LearningRate.contents[0])
    iterations = int(Bs_data.Adam.iteration.contents[0])
    threshold = float(Bs_data.Adam.threshold.contents[0])
    momentum = float(Bs_data.Adam.momentum.contents[0])
    beta1 = float(Bs_data.Adam.beta1.contents[0])
    beta2 = float(Bs_data.Adam.beta2.contents[0])
    all_data_folder = DataFolder
    SplitData.splitImg(all_data_folder)
    training_data_folder = os.path.join('Data', 'Training Data')
    creat_HOD.readImage(training_data_folder)
    start_time = time.time()
    folderImg = os.path.join('Data', os.path.join('Training Data', 'gray'))
    folderHod = os.path.join('Data', os.path.join('Training Data', 'hod'))
    HodTraining = FCM.readHodImg(folderHod) / 255
    ImgTraining = FCM.readDataImg(folderImg) / 255
    for iter in range(0, 30):
        print('iter:  ', iter)
        oPath = "Result" + str(iter)
        try:
            outputPath = os.mkdir(oPath)
        except OSError as error:
            print(error)
        try:
            folderU = 'U' + str(iter)
            folderV = 'V' + str(iter)
            os.mkdir(folderU)
            os.mkdir(folderV)
        except OSError as error:
            print(error)
        FCM.FCM(folderU, folderV, ImgTraining, HodTraining, t, c, m, eps)
        rel_a, rel_b, rel_c, img_a, img_b, img_c = MakeRule.readUAndV(
            folderU, folderV, ImgTraining, HodTraining, c)
        np.savetxt(os.path.join('Rel', 'rel_a.txt'),
                   rel_a,
                   fmt="%.4f",
                   delimiter=',')
        np.savetxt(os.path.join('Img', 'img_a.txt'),
                   img_a,
                   fmt="%.4f",
                   delimiter=',')
        np.savetxt(os.path.join('Rel', 'rel_b.txt'),
                   rel_b,
                   fmt="%.4f",
                   delimiter=',')
        np.savetxt(os.path.join('Img', 'img_b.txt'),
                   img_b,
                   fmt="%.4f",
                   delimiter=',')
        np.savetxt(os.path.join('Rel', 'rel_c.txt'),
                   rel_c,
                   fmt="%.4f",
                   delimiter=',')
        np.savetxt(os.path.join('Img', 'img_c.txt'),
                   img_c,
                   fmt="%.4f",
                   delimiter=',')
        path_rel_a = os.path.join('Rel', 'rel_a.txt')
        path_img_a = os.path.join('Img', 'img_a.txt')
        path_rel_b = os.path.join('Rel', 'rel_b.txt')
        path_img_b = os.path.join('Img', 'img_b.txt')
        path_rel_c = os.path.join('Rel', 'rel_c.txt')
        path_img_c = os.path.join('Img', 'img_c.txt')
        rel_rule, img_rule = TrainingAndPredict.readRule(
            path_rel_a, path_rel_b, path_rel_c, path_img_a, path_img_b,
            path_img_c)
        last_image_name = sorted(
            os.listdir(
                os.path.join('Data', os.path.join('Training Data',
                                                  'gray'))))[-1]
        if (not FCM.validImg(last_image_name)):
            last_image_name = sorted(
                os.listdir(
                    os.path.join('Data', os.path.join('Training Data',
                                                      'gray'))))[-2]
        last_image_path = os.path.join(
            'Data',
            os.path.join('Training Data', os.path.join('gray',
                                                       last_image_name)))
        last_hod_name = sorted(
            os.listdir(
                os.path.join('Data', os.path.join('Training Data',
                                                  'hod'))))[-1]
        if (not FCM.validTxt(last_hod_name)):
            last_hod_name = sorted(
                os.listdir(
                    os.path.join('Data', os.path.join('Training Data',
                                                      'hod'))))[-2]
        last_hod_path = os.path.join(
            'Data',
            os.path.join('Training Data', os.path.join('hod', last_hod_name)))
        U, width, height = TrainingAndPredict.calU(rel_rule, img_rule,
                                                   last_image_path,
                                                   last_hod_path)
        np.savetxt("U_all_rule.txt", U, fmt='%.4f', delimiter=',')
        s = np.ones([U.shape[0], 1])
        for i in range(U.shape[0]):
            if (np.sum(U[i] != 0)):
                s[i] = np.sum(U[i])
        theta1 = np.ones(3)
        first_image_testing = sorted(
            os.listdir(os.path.join('Data', 'Testing')))[0]
        if (not FCM.validImg(first_image_testing)):
            first_image_testing = sorted(
                os.listdir(os.path.join('Data', 'Testing')))[1]
        first_image_testing_path = os.path.join(
            'Data', os.path.join('Testing', first_image_testing))
        y = cv2.imread(first_image_testing_path, 0)  #first image of testing
        y = y.flatten()
        y = y.reshape(-1, 1)
        w = TrainingAndPredict.Adam_img(U, rel_rule, y, theta1, s, lr,
                                        iterations, threshold, momentum, beta1,
                                        beta2, last_image_path)
        for fileName in sorted(os.listdir(os.path.join('Data', 'Testing'))):
            if (FCM.validImg(fileName)):
                rel_W = np.asarray([[1, 2, 1]])
                img_W = w.reshape(1, 3)
                rel_def, img_def = TrainingAndPredict.calDEF(
                    rel_W, img_W, rel_rule, img_rule)
                O_rel, O_img = TrainingAndPredict.calO(rel_def, img_def, U)
                O_rel = ((O_rel / 4) * 255)
                O_img = ((O_img / (np.sum(img_W))) * 255)
                #img=cv2.imread('Hawaii_100x100_crop/Testing/rs_10.jpg',0)
                img = cv2.imread(
                    os.path.join('Data', os.path.join('Testing', fileName)), 0)
                #img=cv2.imread(os.path.join('Data',os.path.join('Testing',...)),0)
                img = img.flatten().reshape(1, -1) / 255
                for i in range(U.shape[0]):
                    if (np.sum(U[i] != 0)):
                        O_rel[0][i] = O_rel[0][i] / np.sum(U[i])
                        O_img[0][i] = O_img[0][i] / np.sum(U[i])
                        O_img[0][i] = img[0][i] * (1 + O_img[0][i])
                O_img = O_img.reshape(width, height).astype(int)
                cv2.imwrite(os.path.join(oPath, fileName), O_img)
                #cv2.imwrite('ver1_'+fileName,O_img)
        result = RMSE.Accuracy(os.path.join('Data', 'Testing'), oPath)
        np.savetxt("./RMSE" + str(iter), result, fmt='%.4f', delimiter=',')
        end_time = time.time()