示例#1
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)
def cluster_border_plot(centers):
    rand_data_number = 1500
    rand_data = create_rand_data(dataa, rand_data_number, dimension_size)
    rand_data = rand_data.T[:len(rand_data.T) - 1].T
    membership = FCM.cal_membership(rand_data, centers, len(centers),
                                    len(rand_data), 2)
    FCM.plot(membership, centers, rand_data)
示例#3
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()
def createSim(fcm, stableList):
    sim = FCM.simulation(fcm)
    sim.steps(100)
    for element in stableList:
        sim.stabilize(element, .001)

    return sim
示例#5
0
 def test_random_digraph_generator(self):
     """tests the random FCM generator function"""
     print "Entering random FCM generator test"
     # generate_random_FCM(node_list, density_percent, seed, node_value_generator, edge_weight_generator)
     seed1 = 5
     seed2 = 7
     node_list = range(0, 6)
     myFCM = FCM.generate_random_FCM(node_list, 0.5, seed2, FCM.pos_real, FCM.pos_neg_real)
示例#6
0
 def test_digraph_to_matrix(self):
     # add some edges to digraph
     digraph = nx.DiGraph(self.simple_digraph)
     edges = [(1, 2, {"weight": 1}), (2, 3, {"weight": 1}), (3, 4, {"weight": 1})]
     digraph.add_edges_from(edges)
     # nx.draw_circular(digraph)
     # plt.show()
     matrix = FCM.digraph_to_matrix(digraph)
     """for row in matrix.keys():
示例#7
0
 def test_random_digraph_generator(self):
     '''tests the random FCM generator function'''
     print "Entering random FCM generator test"
     #generate_random_FCM(node_list, density_percent, seed, node_value_generator, edge_weight_generator)
     seed1 = 5
     seed2 = 7
     node_list = range(0, 6)
     myFCM = FCM.generate_random_FCM(node_list, .5, seed2, FCM.pos_real,
                                     FCM.pos_neg_real)
示例#8
0
def shapeopt(RBF, Kinit, Vinit, iel, LSFip):
    # Variables
    nelx = variables.nelx
    nely = variables.nely
    f = variables.f
    si_max = variables.si_max
    kappa = variables.kappa
    rhomin = variables.rhomin
    edof = variables.edof

    # Discard/free DOFs
    ndof = np.max(edof) + 1
    dofs = np.arange(ndof)
    disc = np.setdiff1d(edof[iel == 0, :], edof[iel != 0, :])
    free = np.setdiff1d(dofs, np.append(variables.fix, disc))
    dofs[np.append(variables.fix, disc)] = -1

    # Optimizer initialization (MMA)
    mma = optimizers.MMA(variables.nele,
                         dmax=si_max,
                         dmin=-si_max,
                         movelimit=0.2,
                         asy=(0.5, 1.2, 0.65),
                         scaling=True)
    x = RBF[:, :, 2].T.reshape(-1).copy()
    iter = 1
    while iter < 10.5:
        # Stiffness matrix
        phi = LSFip.dot(x[:])
        rho = rhomin + (1 - rhomin) * (1 / (1 + np.exp(-kappa * phi)))
        K = FCM.K(Kinit, iel, rho)

        # Reverse Cuthill-McKee ordering
        ind = sp.csgraph.reverse_cuthill_mckee(K, symmetric_mode=True)
        free = dofs[ind][dofs[ind] != -1]

        # Solve (reduced) system
        u = np.zeros((np.size(K, 0), ))
        u[free] = solver(K[free, :][:, free], f[free])

        # Compliance objective and its sensitivity
        [C, dC] = obj(Kinit, iel, LSFip, u, phi, rho, f, free)

        # Volume fraction constraint and its sensitivity
        [g, dg] = constr(Vinit, iel, LSFip, phi, rho)

        # Shape optimization
        dx = mma.solve(x, C, dC, g, dg, iter)
        x = x + dx

        # Update log
        print(['%.3f' % i for i in [iter, C, g]])
        iter += 1

    RBF[:, :, 2] = x.reshape(nely, nelx).T
    return RBF
示例#9
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
示例#10
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)
def LSF(variables, RBF):
    nele = variables.nele
    nod = variables.nod
    ele = variables.ele

    # Gauss quadrature
    [pnt, wgt] = FCM.Gauss_scheme(1, 2)  #P=1, dim=2D

    # Numerical integration to compute volume
    phi = np.zeros((nele * len(wgt), ))
    for e in range(nele):
        for gp in range(len(wgt)):  #for the number of int points in element
            N = FCM.N(pnt[gp, :], 1)  #P=1
            x, y = np.dot(N, nod[ele[e, :], :])  #loc int point in global coord
            phi[e * len(wgt) + gp] = LSFeval(variables, RBF, x,
                                             y)  #LSF at int point

    wgts = np.tile(wgt, nele)

    return phi, wgts
示例#13
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
示例#14
0
def parallelizeS(fcm, stabilizers):
    sim = FCM.simulation(fcm)

    for key in stabilizers:
        sim.stabilize(key, stabilizers[key])

    #simulate until 10000 steps or stability reached
    sim.steps(10000)
    sim.changeTransferFunction(lambda x: 1 / (1 + math.exp(-x)))
    values = sim.run()
    return values
示例#15
0
    def test_matrix_to_digraph(self):
        #add some edges to matrix
        matrix = copy.deepcopy(self.simple_matrix)
        matrix[1][2] = 1
        matrix[1][3] = 1
        matrix[1][4] = 1
        matrix[2][3] = 1
        #print out matrix with weighted edges
        '''for row in matrix.keys():
            print row, matrix[row]'''

        digraph = FCM.matrix_to_digraph(matrix)
示例#16
0
    def test_matrix_to_digraph(self):
        # add some edges to matrix
        matrix = copy.deepcopy(self.simple_matrix)
        matrix[1][2] = 1
        matrix[1][3] = 1
        matrix[1][4] = 1
        matrix[2][3] = 1
        # print out matrix with weighted edges
        """for row in matrix.keys():
            print row, matrix[row]"""

        digraph = FCM.matrix_to_digraph(matrix)
示例#17
0
class TestFCM(unittest.TestCase):
    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)

    def test_isScoreValid(
            self):  # Controlla che l'accuratezza sia un valore valido
        n_clusters = 2
        train_membership, centers = self.model.fuzzy_train(
            self.X_train, n_clusters, 2)
        test_membership = self.model.fuzzy_predict(self.X_test, n_clusters,
                                                   centers, 2)
        #warnings.filterwarnings("ignore")
        acc = self.model.accuracy(test_membership, self.y_test)
        self.assertGreaterEqual(acc, 0)
        self.assertLessEqual(acc, 1)

    def test_errorThreshold(
        self
    ):  # Controlla che l'errore RMSE delle membership sia minore di una certa soglia
        threshold = 0.4
        n_clusters = 2
        train_membership, centers = self.model.fuzzy_train(
            self.X_train, n_clusters, 2)
        test_membership = self.model.fuzzy_predict(self.X_test, n_clusters,
                                                   centers, 2)
        #warnings.filterwarnings("ignore")
        err = self.model.RMSE_membership(test_membership, self.y_test)
        self.assertLessEqual(err, threshold)
示例#18
0
    def Kmeans_segment(self, ):
        # segment full tumor
        X = (self.flair - self.flair.mean()) / self.flair.std()
        model = FCM.siFCM(X, 5)
        model.FCM_cluster()
        full_label = np.zeros(X.shape)
        full_label[model.result == np.argmax(model.C)] = 1
        full_tumor = np.reshape(full_label, self.shape)

        # 填充连通域,获取boundingbox
        self.full_label, self.full_label_bbox = self.element_Choose(full_tumor)
        self.full_label = self.fill_full(self.full_label)
        # 新的分割掩膜

        self.t1ce[self.full_label == 0] = 0
        self.t1ce = self.t1ce[self.full_label_bbox[0]:self.full_label_bbox[3],
                              self.full_label_bbox[1]:self.full_label_bbox[4],
                              self.full_label_bbox[2]:self.full_label_bbox[5]]

        # segment the enhance tumor
        X2 = (self.t1ce - self.t1ce.mean()) / self.t1ce.std()
        model2 = FCM.siFCM(X2, 3)
        model2.FCM_cluster()
        enhance_tumor = np.zeros(X2.shape)
        enhance_tumor[model2.result == np.argmax(model2.C)] = 1
        enhance_tumor = np.reshape(enhance_tumor, self.t1ce.shape)
        # 找到最大连通域的b_box
        enhance_label, _ = self.element_Choose(enhance_tumor)
        self.enhance_label[
            self.full_label_bbox[0]:self.full_label_bbox[3],
            self.full_label_bbox[1]:self.full_label_bbox[4],
            self.full_label_bbox[2]:self.full_label_bbox[5]] = enhance_label

        # segment the necrotic
        self.necrotic_label = self.segment_necrotic(self.enhance_label)

        self.all_label[self.full_label == 1] = 2
        self.all_label[self.enhance_label == 1] = 4
        self.all_label[self.necrotic_label == 1] = 1
def train(train_dataa, train_dataa_size, cluster_size, iteration_size,
          dimension_size, m, gama):
    """ remove class column"""
    train_data = train_dataa.T[:len(train_dataa.T) - 1].T

    centers, memberShip_train = FCM.fcm(train_data, train_dataa_size,
                                        cluster_size, iteration_size,
                                        dimension_size, m)
    """ to find out what G and W are see project definition"""
    G = cal_G(train_data, centers, memberShip_train, gama, train_dataa_size)
    W = cal_w(G, train_dataa, cluster_size, len(train_dataa))

    return W, centers
def test(test_dataa, test_dataa_size, cluster_size, m, centers, W, gama):
    test_data = test_dataa.T[:len(test_dataa.T) - 1].T
    membership_test = FCM.cal_membership(test_data, centers, cluster_size,
                                         test_dataa_size, m)
    G = cal_G(test_data, centers, membership_test, gama, test_dataa_size)
    G = np.array(G)
    yh = G @ W
    yhat = []
    for r in range(0, test_dataa_size):
        argmax = np.argmax(yh[r])
        yhat.append(argmax)
        # test_data[r].append(argmax)

    scatter(test_dataa)
    # scatter(test_data)
    plt.figure()

    f1 = f2 = 0
    for uo in range(0, len(test_data)):
        if yhat[uo] == 1:
            if f1 == 0:
                plt.scatter(test_data[uo][0],
                            test_data[uo][1],
                            color='red',
                            label='Label=1')
                f1 = 1
            else:
                plt.scatter(test_data[uo][0], test_data[uo][1], color='red')
        else:
            if f2 == 0:
                plt.scatter(test_data[uo][0],
                            test_data[uo][1],
                            color='blue',
                            label='Label=0')
                f2 = 1
            else:
                plt.scatter(test_data[uo][0], test_data[uo][1], color='blue')

    plt.title('Scattered data!')
    plt.legend(loc="upper left")
    plt.show()

    accuracy = 0
    for ty in range(0, test_dataa_size):
        if (test_dataa[ty][2] == 1.0
                and yhat[ty] == 1.0) or (test_dataa[ty][2] == -1.0
                                         and yhat[ty] == 0):
            accuracy = accuracy + 1
    accuracy = accuracy / test_dataa_size * 100
    print('accuracy is ', accuracy)
示例#21
0
 def test_digraph_to_matrix(self):
     #add some edges to digraph
     digraph = nx.DiGraph(self.simple_digraph)
     edges = [(1, 2, {
         'weight': 1
     }), (2, 3, {
         "weight": 1
     }), (3, 4, {
         "weight": 1
     })]
     digraph.add_edges_from(edges)
     #nx.draw_circular(digraph)
     #plt.show()
     matrix = FCM.digraph_to_matrix(digraph)
     '''for row in matrix.keys():
示例#22
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())
示例#23
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
 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)
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
示例#26
0
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)
示例#27
0
#%%
""" Extract a geometry using an LSF """
print("Stage 2 - Geometry extraction")

# Stage 2 - Obtain initial geometry
RBF = stage2.geomextr(dens)

# Visualize stage 2
visualize.stage2_3(RBF,5,2)

#%%
""" Optimize the shape of the geometry """
print("Stage 3 - Shape optimization")
# Create stiffness matrices (per integration point)
Kinit,Vinit,ic = FCM.Kinit()

# Stage 3 - Shape optimization
steps = 1
while steps<1.5:
    
    # Create quadtree integration band
    print("- (re)create quadtree integration band")
    iel,LSFip = FCM.quadtree_band(RBF,ic)
    
    # Shape optimization
    RBF = stage3.shapeopt(RBF,Kinit,Vinit,iel,LSFip)
    steps += 1
    
# Visualize stage 3
visualize.stage2_3(RBF,5,3)
示例#28
0
import file_handler
import random
import FCM
import numpy as np
if __name__ == '__main__':
    data = file_handler.read_file('input_data/2clstrain1200.csv')

    input_size = len(data)
    train_data = []
    test_data = []
    # producing random data for final drawing X:[-5, 17.5], Y:[-2.5, 15]
    X_rand, Y_rand = FCM.produce_random_num(-8, 19, -4, 17, 2000)

    random_input = np.concatenate((X_rand, Y_rand), axis=0)

    # Here we should choose randomly train and test data
    train_size = int(70 * input_size / 100)
    test_size = input_size - train_size

    train_data = [data[i] for i in range(test_size, input_size)]
    test_data = [data[i] for i in range(0, test_size)]

    train_data_no_label = []
    train_labels = []

    test_data_no_label = []
    test_labels = []

    for i in range(len(train_data)):
        train_data_no_label.append([train_data[i][0], train_data[i][1]])
        train_labels.append(train_data[i][2])
示例#29
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)
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()
示例#31
0
文件: f2.py 项目: anvesh2502/Leetcode
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'
示例#32
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()
示例#33
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)
示例#34
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)