示例#1
0
 def get_str_variables(strVariables, variables):
     for var in strVariables:
         if (not os.path.exists(file_path + tid + "_" + var + ".str")):
             print("File:{} does not exist".format(file_path + tid + "_" +
                                                   var + ".str"))
             return variables
         lines = util.load_array(file_path + tid + "_" + var + ".str",
                                 False)
         variables[var] = lines
示例#2
0
 def getValue():
     headValue = {}
     numSize = 0
     for i in range(0, headLength):
         if (headType[i][0] == 1 and headType[i][1] == 0):  #Only string
             headValue[numSize] = restore_diff(
                 util.load_array(
                     util.decoder(path + "Head" + str(numSize) + ".head",
                                  is_encoder)), is_diff)
             numSize += 1
             continue
         for t in range(0, headType[i][1]):  #Number and string
             headValue[numSize] = restore_diff(
                 util.load_array(
                     util.decoder(path + "Head" + str(numSize) + ".head",
                                  is_encoder)), is_diff)
             numSize += 1
     return headValue
示例#3
0
 def get_num_variables(numVariables, variables):
     for var in numVariables:
         if (not os.path.exists(file_path + tid + "_" + var + ".dat")):
             print("File:{} does not exist".format(file_path + tid + "_" +
                                                   var + ".dat"))
             return variables
         array = util.load_array(
             util.decoder(file_path + tid + "_" + var + ".dat", is_encoder),
             False)
         variables[var] = array
示例#4
0
def restore_variables(template_path, file_path, tid,
                      level):  #Restore varibales for each template
    variables = {}
    template_level = level[_TemplateLevel]
    is_encoder = level[_EncoderLevel]

    def get_str_variables(strVariables, variables):
        for var in strVariables:
            if (not os.path.exists(file_path + tid + "_" + var + ".str")):
                print("File:{} does not exist".format(file_path + tid + "_" +
                                                      var + ".str"))
                return variables
            lines = util.load_array(file_path + tid + "_" + var + ".str",
                                    False)
            variables[var] = lines

    def get_num_variables(numVariables, variables):
        for var in numVariables:
            if (not os.path.exists(file_path + tid + "_" + var + ".dat")):
                print("File:{} does not exist".format(file_path + tid + "_" +
                                                      var + ".dat"))
                return variables
            array = util.load_array(
                util.decoder(file_path + tid + "_" + var + ".dat", is_encoder),
                False)
            variables[var] = array

    #Read basic rules
    basicRules = util.load_array(
        os.path.join(template_path, tid + "basic.rule"), False)
    [v_tot, n_tot, s_tot] = basicRules[0].split()
    numVariables = []
    strVariables = []
    if (int(v_tot) == 0):
        return variables
    if (int(n_tot) != 0):
        numVariables = basicRules[1].split()
    if (int(s_tot) != 0):
        strVariables = basicRules[2].split()
    #String variables
    get_str_variables(strVariables, variables)
    #Numerial variables
    get_num_variables(numVariables, variables)
    return variables
def synthesize_shape(attributes, c=0, model_name='KPCA'):
    '''
    attributes : array_like
        Values of shape attributes which have the range [0, 1].
    model_name : str
        Name of the trained model.
    c : int
        The index of a cluster
    X : array_like
        Reconstructed high-dimensional design parameters
    '''
    
    config = ConfigParser.ConfigParser()
    config.read('config.ini')
    source = config.get('Global', 'source')
    
    dim = attributes.shape[1]    
    
    transforms = [load_model(model_name+'_fpca', c)]
    transforms.append(load_model(model_name+'_fscaler', c))
    
    model = load_model(model_name, c)
    
    xpca = load_model('xpca', c)
    dim_increase = xpca.inverse_transform
    
    if dim > 1:
        boundary = load_array(model_name+'_boundary', c)
    else:
        boundary = None
    
    # Get save directory
    save_dir = get_fname(model_name, c, directory='./synthesized_shapes/', extension='png')
    
    print('Plotting synthesized shapes for %s_%d ... ' % (model_name, c))
    if dim < 4:            
        if source == 'glass':
            X = plot_synthesis(attributes, model.inverse_transform, dim_increase, transforms, save_dir, 
                               model_name, boundary)
        else:
            X = plot_synthesis(attributes, model.inverse_transform, dim_increase, transforms, save_dir, 
                               model_name, boundary, mirror=False)
                              
    np.save(get_fname(model_name, c, directory='./synthesized_shapes/', extension='npy'), X)
示例#6
0
    return pool, bounds_new


if __name__ == "__main__":

    np.random.seed(0)

    model_name = 'PCA'
    c = 0

    n_iter = 1000

    length_scale = 1.3
    margin = .7  # higher -> emphasize more on variance -> more exploration/less exploit

    T = load_array(model_name + '_f', c)  # Training samples
    d = T.shape[1]

    # Set a global boundary
    BD = np.array([[-10, -30, -30], [60, 30, 30]])

    # Initial valid samples
    kv = 10  # number of initial valid samples
    Dv = T[select_diverse(T, kv)]
    Xv = synthesize_shape(Dv, c=c, model_name=model_name, raw=True)
    save_plot(Dv, Xv, c=c, model_name=model_name + '_initial')
    # Initial invalid samples
    ki = 1  # number of initial invalid samples
    mn = np.min(T, axis=0)
    mx = np.max(T, axis=0)
    Di = gen_intersect(ki, d, mn, mx)
示例#7
0
def restore_heads(path, tempalte_path, level_msg):
    def restore_diff(array, is_diff):
        if not is_diff:
            return array
        new_array = []
        new_array.append(array[0])
        now_size = array[0]
        for n in range(1, len(array)):
            now_size += array[n]
            new_array.append(now_size)
        return new_array

    def restore_dict(array, dict):
        new_array = []
        for i in array:
            new_array.append(dict[i])
        return new_array

    def build_dict(array):
        dic = dict()
        for idx, a in enumerate(array):
            dic[idx] = a
        return dic

    def getValue():
        headValue = {}
        numSize = 0
        for i in range(0, headLength):
            if (headType[i][0] == 1 and headType[i][1] == 0):  #Only string
                headValue[numSize] = restore_diff(
                    util.load_array(
                        util.decoder(path + "Head" + str(numSize) + ".head",
                                     is_encoder)), is_diff)
                numSize += 1
                continue
            for t in range(0, headType[i][1]):  #Number and string
                headValue[numSize] = restore_diff(
                    util.load_array(
                        util.decoder(path + "Head" + str(numSize) + ".head",
                                     is_encoder)), is_diff)
                numSize += 1
        return headValue

    def pendding(num, length):
        if length == -1:
            return str(num)
        else:
            temp = str(num)
            if len(temp) < length:
                temp = "0" + temp
            return temp

    is_diff = level_msg[_DiffLevel]
    is_encoder = level_msg[_EncoderLevel]
    headLength, isMulti, headRegex, headType, headFormat, headString, headNumber, headDelimer = get_info(
        os.path.join(template_path, "head.format"))
    print(headType)
    HeadDict = build_dict(
        util.load_array(os.path.join(path, "Header_dictionary.headDict"),
                        False))
    HeadValue = getValue()
    totLength = len(HeadValue[0])
    for key in HeadValue.keys():
        assert (len(HeadValue[key]) == totLength)
    now_num = 0
    now_str = 0
    Heads = ["" for i in range(0, totLength)]
    #Restore each part
    for i in range(0, headLength):
        if (headType[i][0] == 1 and headType[i][1] == 0):
            print(i)
            print(now_num)
            #print(HeadValue[8])
            part = restore_dict(HeadValue[now_num], HeadDict)
            Heads = [
                Heads[t] + part[t] + headDelimer[i]
                for t in range(0, totLength)
            ]
            now_num += 1
            continue
        now_format = headFormat[i]
        fidx = 0
        while fidx < len(now_format):
            if (now_format[fidx] == '%' and now_format[fidx + 1] == 'd'):  #Num
                Heads = [
                    Heads[t] +
                    pendding(HeadValue[now_num][t], headNumber[now_num])
                    for t in range(0, totLength)
                ]
                now_num += 1
                fidx += 2
            else:  #String start
                Heads = [
                    Heads[t] + now_format[fidx] for t in range(0, totLength)
                ]
                fidx += 1
        Heads = [Heads[t] + headDelimer[i] for t in range(0, totLength)]
    return Heads, [isMulti, headRegex]
示例#8
0
    #Load match_failed log
    MatchFailed = load_log(os.path.join(temp_path, "match_failed.log"),
                           head_msg)

    print("Load failed #: {}, Match failed #:{}".format(
        len(LoadFailed), len(MatchFailed)))
    #Use Eid, load_failed, match_failed, Tempaltes, Index, Variables
    match_idx = 0
    load_idx = 0
    head_idx = 0
    template_idx = {}
    for t in Templates.keys():
        template_idx[t] = 0

    Eids = util.load_array(
        util.decoder(os.path.join(temp_path, "Eid.eid"),
                     level_msg[_EncoderLevel]))
    fw = open(
        os.path.join(output_path,
                     util.get_file_name(input_path) + ".col"), "wb")
    for eid in Eids:
        if (eid == -1):
            fw.write((LoadFailed[load_idx] + '\n').encode())
            #print(load_idx)
            load_idx += 1
            continue
        if (eid == 0):
            #try:
            fw.write((MatchFailed[match_idx] + '\n').encode())
            #except:
            #print("tot length: {}, match index: {}".format(len(MatchFailed), match_idx))