示例#1
0
文件: mlst.py 项目: dseeto/mlst
def experiment(edge_set, mlst_handler, experiment_name="Experiment:", experiment_desc=None, display=False):
    """
    Runs the experiment with a given handler and edge set
    """

    # run the experiment
    print ">>> %s" %(str(experiment_name)); stime = time.time()
    print "--------------------------------------------------------------"

    output_edge_set = mlst_handler.find_mlst(edge_set)

    assert util.is_mlst(output_edge_set) != False

    print "--------------------------------------------------------------"

    etime = time.time(); duration = int((etime - stime) * 1000)
    output_graph = graph.make_graph(output_edge_set)
    output_graph.search()
    stats = output_graph.stats()

    print ">>> Average Degree: %s" %(str(stats["average_degree"]))
    print ">>> Number of Leaves: %s" %(str(output_graph.num_leaves))
    print "<<< Time Elapsed: %d ms" %(duration)
    print "\n"

    # display input/output graph
    if display: util.display(edge_set)
    if display: util.display(output_edge_set)

    stats = {}
    stats["duration"] = duration
    stats["output_edge_set"] = output_edge_set
    return stats
示例#2
0
def display(values):
    """
    Display the values as a 2-D grid.
    Args:
        values(dict): The sudoku in dictionary form
    """
    util.display(values)
示例#3
0
def solve(size, start_game_board):
    start_locdict = convert2locdict(start_game_board)
    size_sq = size**2
    display(start_locdict, size)
    paths = {}
    for coin, locs in start_game_board.items():
        paths[coin] = []
        for path in get_paths(coin, start_locdict.copy(), size, [locs[0]],
                              locs[-1]):
            paths[coin].append(path)
    paths = remove_obviously_wrong(paths, size_sq)
    combo_len = len(paths.keys())
    locdict = None
    count = 0
    for combo in product(*paths.values()):
        total_length = sum([len(path) for path in combo])
        count += 1
        if count % 100000 == 0:
            print "checked %d combos, tl: %d" % (count, total_length)
        if total_length == size_sq:
            possible_gb = convert2gameboard(start_locdict, combo)

            locdict = convert2locdict(possible_gb, raise_error=False)
            if locdict:
                print "possible combo", combo
                break
        else:
            continue
    print "total %d combos checked" % count
    if locdict:
        display(locdict, size)
    else:
        print "can't find solution."
示例#4
0
def freeze(filename, icon=None, hidden=None):
    """
    Compile a Python file into a standalone executable
    binary with a built-in Python interpreter

    `Required`
    :param str icon:        icon image filename
    :param str filename:    target filename

    Returns output filename as a string

    """
    global template_spec
    basename = os.path.basename(filename)
    name = os.path.splitext(basename)[0]
    path = os.path.splitdrive(os.path.abspath('.'))[1].replace('\\','/')
    key = ''.join([random.choice([chr(i) for i in list(range(48,91)) + list(range(97,123))]) for _ in range(16)])

    imports = []
    with open(filename) as import_file:
        for potental_import in filter(None, (PI.strip().split() for PI in import_file)):
            if potental_import[0] == 'import':
                imports.append(potental_import[1].split(';')[0].split(','))

    bad_imports = set()
    bad_imports.add('core')
    for i in os.listdir('core'):
        i = os.path.splitext(i)[0]
        bad_imports.add(i)
        bad_imports.add('core.%s' % i)

    for imported in imports:
        if isinstance(imported, list):
            __ = imports.pop(imports.index(imported))
            for ___ in __:
                if ___ not in bad_imports:
                    imports.append(___)

    imports = list(set(imports))
    if isinstance(hidden, list):
        imports.extend(hidden)
    spec = template_spec.substitute(KEY=repr(key), BASENAME=repr(basename), PATH=repr(path), IMPORTS=imports, NAME=repr(name), ICON=repr(icon))
    fspec = os.path.join(path, name + '.spec')
    with open(fspec, 'w') as fp:
        fp.write(spec)
    process = subprocess.Popen('{} -m PyInstaller {}'.format(sys.executable, fspec), 0, None, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, shell=True)
    while True:
        try:
            line = process.stderr.readline().rstrip()
        except: 
            break
        if line.strip() != None:
            util.display(line, color='reset', style='dim')
            line = line.decode('utf-8')
            if 'EXE' in line and 'complete' in line:
                break
        time.sleep(0.25)
    output = os.path.join(path, 'dist', name + str('.exe' if os.name == 'nt' else ''))
    return output
示例#5
0
文件: generators.py 项目: hmz777/byob
def freeze(filename, icon=None, hidden=None):
    """
    Compile a Python file into a standalone executable
    binary with a built-in Python interpreter

    `Required`
    :param str icon:        icon image filename
    :param str filename:    target filename

    Returns output filename as a string

    """
    global template_spec
    basename = os.path.basename(filename)
    name = os.path.splitext(basename)[0]
    path = os.path.splitdrive(os.path.abspath('.'))[1].replace('\\','/')
    key = ''.join([random.choice([chr(i) for i in list(range(48,91)) + list(range(97,123))]) for _ in range(16)])

    imports = []
    with open(filename) as import_file:
        for potental_import in filter(None, (PI.strip().split() for PI in import_file)):
            if potental_import[0] == 'import':
                imports.append(potental_import[1].split(';')[0].split(','))

    bad_imports = set()
    bad_imports.add('core')
    for i in os.listdir('core'):
        i = os.path.splitext(i)[0]
        bad_imports.add(i)
        bad_imports.add('core.%s' % i)

    for imported in imports:
        if isinstance(imported, list):
            __ = imports.pop(imports.index(imported))
            for ___ in __:
                if ___ not in bad_imports:
                    imports.append(___)

    imports = list(set(imports))
    if isinstance(hidden, list):
        imports.extend(hidden)
    spec = template_spec.substitute(KEY=repr(key), BASENAME=repr(basename), PATH=repr(path), IMPORTS=imports, NAME=repr(name), ICON=repr(icon))
    fspec = os.path.join(path, name + '.spec')
    with open(fspec, 'w') as fp:
        fp.write(spec)
    process = subprocess.Popen('{} -m PyInstaller {}'.format(sys.executable, fspec), 0, None, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, shell=True)
    while True:
        try:
            line = process.stderr.readline().rstrip()
        except: 
            break
        if line.strip() != None:
            util.display(line, color='reset', style='dim')
            line = line.decode('utf-8')
            if 'EXE' in line and 'complete' in line:
                break
        time.sleep(0.25)
    output = os.path.join(path, 'dist', name + str('.exe' if os.name == 'nt' else ''))
    return output
示例#6
0
def layout():
  # The following command does not work well
  # select-layout tiled => Always splits a window vertically first, so does
  #    not save the direction.
  count, w, h = util.display(['window_panes', 'client_width', 'client_height'])
  env = util.harukam_env()

  m = {
    "HOME_DESKTOP": {
      4: "0c9d,272x64,0,0{136x64,0,0[136x32,0,0,14,136x31,0,33,17],135x64,137,0[135x32,137,0,15,135x31,137,33,16]}",
      6: "b6cc,272x64,0,0{90x64,0,0[90x32,0,0,6,90x31,0,33,4],90x64,91,0[90x32,91,0,7,90x31,91,33,2],90x64,182,0[90x32,182,0,5,90x31,182,33,3]}"
    },
    "HOME_ASUS": {
      4: "90ea,272x62,0,0{136x62,0,0[136x31,0,0,13,136x30,0,32,16],135x62,137,0[135x31,137,0,14,135x30,137,32,15]}",
      6: "188a,272x62,0,0{90x62,0,0[90x31,0,0,7,90x30,0,32,10],90x62,91,0[90x31,91,0,8,90x30,91,32,11],90x62,182,0[90x31,182,0,9,90x30,182,32,12]}"
    },
    "HOME_MAC": {
      4: "49f3,238x54,0,0{119x54,0,0[119x27,0,0,13,119x26,0,28,16],118x54,120,0[118x27,120,0,14,118x26,120,28,15]}",
      6: "9de9,238x54,0,0{78x54,0,0[78x27,0,0,7,78x26,0,28,10],78x54,79,0[78x27,79,0,8,78x26,79,28,11],80x54,158,0[80x27,158,0,9,80x26,158,28,12]}"
    },
    "OFFICE": {
      4: "93cb,245x79,0,0{122x79,0,0[122x39,0,0,13,122x39,0,40,16],122x79,123,0[122x39,123,0,14,122x39,123,40,15]}",
      6: "dddc,245x79,0,0{81x79,0,0[81x39,0,0,7,81x39,0,40,10],81x79,82,0[81x39,82,0,8,81x39,82,40,11],81x79,164,0[81x39,164,0,9,81x39,164,40,12]}"
    }
  }

  util.exit0_ifnot(env, "no env")
  util.exit0_ifnot(env in m, "unknown env: " + env)
  util.exit0_ifnot(count in m[env], "not supported count: " + str(count))

  layout = m[env][count]
  exitcode, _ = util.run_cmd(['tmux', 'select-layout', layout])

  util.exit0_ifnot(exitcode == 0, "select-layout failed")
示例#7
0
文件: solver.py 项目: dseeto/mlst
def generate_nbit_graph(nbit_generator):

    # before
    base_edge_set = nbit_generator.base_edges
    #util.display(base_edge_set)

    # generates 
    original_edge_set, mlst_edge_set = nbit_generator.generate_graph()
    util.display(mlst_edge_set)
    util.display(original_edge_set)

    # make sure that the degree is the same
    #for i in range(nbit_generator.num_vertices):
        #neighbors = nbit_generator.graph.neighbors[i]
        #assert len(neighbors) == nbit_generator.degree, "neighbors are not all %d!!" %(nbit_generator.degree)
    
    return original_edge_set, mlst_edge_set 
示例#8
0
def _main():
    from util import display
    from mdp.mdp import GridWorldMDP

    init = 0
    goal = 35
    default_reward = -2
    beta = 10
    g = GridWorldMDP(6,
                     6,
                     default_reward=default_reward,
                     euclidean_rewards=True)

    traj = simulate(g, 0, goal, beta=beta)
    print "Testing hardmax.simulate:"
    print "  * default_reward={}, beta={}".format(default_reward, beta)
    print "  * traj: {}".format([(g.state_to_coor(s), g.Actions(a))
                                 for s, a in traj])
    display(g, traj, init, goal, overlay=True)
示例#9
0
    def play_player_turn(self, deck, player_hand):
        print("\nStand or Hit? [s/h]")
        player_choice = input()

        if player_choice == "s":
            util.display("\nYou chose to stand.")
            choice_invalid = False

        elif player_choice == "h":
            util.display("\nYou chose to hit!")

            new_card = deck.draw()
            print("\nYou drew ", end="")
            util.print_hand([new_card], 1)

            player_hand.append(new_card)
            choice_invalid = False

        else:
            print("\nNot a valid choice.")
            return True
示例#10
0
def exe(filename, icon=None, hidden=None):
    """ 
    Compile the Python stager file into a standalone executable
    with a built-in Python interpreter

    `Required`
    :param str icon:        icon image filename
    :param str filename:    target filename

    Returns output filename as a string
    
    """
    basename = os.path.basename(filename)
    name = os.path.splitext(basename)[0]
    path = os.path.splitdrive(os.path.abspath('.'))[1].replace('\\','/')
    key = str().join([random.choice([chr(i) for i in range(48,91) + range(97,123)]) for _ in range(16)])
    imports = [i.strip().split()[1].split(';')[0].split(',') for i in open(filename).read().splitlines() if len(i.strip().split()) if i.strip().split()[0] == 'import']
    for _ in imports:
        if isinstance(_, list):
            __ = imports.pop(imports.index(_))
            for ___ in __:
                if ___ not in ['core'] + [os.path.splitext(i)[0] for i in os.listdir('core')] + ['core.%s' % s for s in [os.path.splitext(i)[0] for i in os.listdir('core')]]:
                    imports.append(___)
    imports = list(set(imports))
    if isinstance(hidden, list):
        imports.extend(hidden)
    spec = __Template_spec.format(key=repr(key), basename=repr(basename), path=repr(path), imports=imports, name=repr(name), icon=repr(icon))
    fspec = os.path.join(path, name + '.spec')
    with file(fspec, 'w') as fp:
        fp.write(spec)
    process = subprocess.Popen('{} -m PyInstaller {}'.format(sys.executable, fspec), 0, None, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, shell=True)
    while True:
        try:
            line = process.stdout.readline().rstrip()
        except: break
        if line: util.display(line, color='reset', style='dim')
        if 'EXE' in line and 'complete' in line: break
    output = os.path.join(path, 'dist', name + str('.exe' if os.name == 'nt' else ''))
    return output
示例#11
0
def freeze(filename, icon=None, hidden=None):
    """ 
    Compile a Python file into a standalone executable
    binary with a built-in Python interpreter

    `Required`
    :param str icon:        icon image filename
    :param str filename:    target filename

    Returns output filename as a string
    
    """
    basename = os.path.basename(filename)
    name = os.path.splitext(basename)[0]
    path = os.path.splitdrive(os.path.abspath('.'))[1].replace('\\','/')
    key = str().join([random.choice([chr(i) for i in range(48,91) + range(97,123)]) for _ in range(16)])
    imports = [i.strip().split()[1].split(';')[0].split(',') for i in open(filename).read().splitlines() if len(i.strip().split()) if i.strip().split()[0] == 'import']
    for _ in imports:
        if isinstance(_, list):
            __ = imports.pop(imports.index(_))
            for ___ in __:
                if ___ not in ['core'] + [os.path.splitext(i)[0] for i in os.listdir('core')] + ['core.%s' % s for s in [os.path.splitext(i)[0] for i in os.listdir('core')]]:
                    imports.append(___)
    imports = list(set(imports))
    if isinstance(hidden, list):
        imports.extend(hidden)
    spec = __Template_spec.format(key=repr(key), basename=repr(basename), path=repr(path), imports=imports, name=repr(name), icon=repr(icon))
    fspec = os.path.join(path, name + '.spec')
    with file(fspec, 'w') as fp:
        fp.write(spec)
    process = subprocess.Popen('{} -m PyInstaller {}'.format(sys.executable, fspec), 0, None, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, shell=True)
    while True:
        try:
            line = process.stderr.readline().rstrip()
        except: break
        if line: util.display(line, color='reset', style='dim')
        if 'EXE' in line and 'complete' in line: break
    output = os.path.join(path, 'dist', name + str('.exe' if os.name == 'nt' else ''))
    return output
示例#12
0
    def play_cpu_turn(self, deck, cpu_hand):
        util.display("\nJack Black is deciding...")
        cpu_should_hit = self.should_hit(cpu_hand)
        if cpu_should_hit:
            util.display("\nJack Black chose to hit!")

            new_card = deck.draw()
            print("\nJack Black drew ", end="")
            self.print_hand([new_card], 1)

            cpu_hand.append(new_card)
        else:
            util.display("\nJack Black chose to stand.")
示例#13
0
def jianpu_to_midi(img_path):
    original = cv.imread(img_path)
    assert original is not None, 'img_path does not exist'

    roi = util.page_detect_contour(original)
    assert roi is not None, 'page does not exist'
    roi = cv.cvtColor(roi, cv.COLOR_BGR2GRAY)

    # blurred = cv.GaussianBlur(roi, (3, 3), 0)
    adjusted = util.bleach_shadows(roi)

    # binarized = cv.adaptiveThreshold(blurred, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 8)
    _, binarized = cv.threshold(adjusted, 0, 255,
                                cv.THRESH_BINARY + cv.THRESH_OTSU)
    binarized = cv.bitwise_not(binarized)
    row_imgs, row_binaries, row_ranges = util.dissect_rows(adjusted, binarized)

    # obj_dict = util.dissect_objects(row_imgs[8], row_binaries[8])
    # line = AbstractLine.construct(row_imgs[8], obj_dict)
    # # line.visualize()
    # print(line)
    # for obj_index in line.dots:
    #     util.display(str(obj_index), obj_dict[obj_index])

    lines = []
    index = 0
    for img, binary in zip(row_imgs, row_binaries):
        try:
            obj_dict = util.dissect_objects(img, binary)
            line = AbstractLine.construct(img, obj_dict)
            lines.append(line)
            print(index)
            print(line)
        except Exception:
            traceback.print_exc()
        index += 1

    util.display('Original', original)
    util.display('Binarized', np.hstack((adjusted, binarized)))
    util.display(
        'Rows',
        np.hstack(
            (util.bordered_stack(row_imgs,
                                 0), util.bordered_stack(row_binaries, 0))))

    cv.waitKey(0)
    cv.destroyAllWindows()

    return binarized
示例#14
0
common_params = dict(sampling_freq=22050, clip_duration=16000, frame_duration=16)
train_params = AudioParams(**common_params)
valid_params = AudioParams(**common_params)
common = dict(target_size=1, nclasses=10, repo_dir=args.data_dir)
train = DataLoader(set_name='music-train', media_params=train_params,
                   index_file=train_idx, shuffle=True, **common)
valid = DataLoader(set_name='music-valid', media_params=valid_params,
                   index_file=valid_idx, shuffle=False, **common)
init = Gaussian(scale=0.01)
layers = [Conv((2, 2, 4), init=init, activation=Rectlin(),
               strides=dict(str_h=2, str_w=4)),
          Pooling(2, strides=2),
          Conv((3, 3, 4), init=init, batch_norm=True, activation=Rectlin(),
               strides=dict(str_h=1, str_w=2)),
          DeepBiRNN(128, init=GlorotUniform(), batch_norm=True, activation=Rectlin(),
                    reset_cells=True, depth=3),
          RecurrentMean(),
          Affine(nout=common['nclasses'], init=init, activation=Softmax())]

model = Model(layers=layers)
opt = Adagrad(learning_rate=0.01, gradient_clip_value=15)
metric = Misclassification()
callbacks = Callbacks(model, eval_set=valid, metric=metric, **args.callback_args)
cost = GeneralizedCost(costfunc=CrossEntropyMulti())

model.fit(train, optimizer=opt, num_epochs=args.epochs, cost=cost, callbacks=callbacks)
print('Misclassification error = %.1f%%' % (model.eval(valid, metric=metric)*100))
display(model, ['Convolution_0'], 'inputs')
display(model, ['Convolution_0', 'Convolution_1', 'Pooling_0'], 'outputs')
示例#15
0
文件: database.py 项目: datag00n/byob
 def _display(self, data, indent=4):
     if isinstance(data, dict):
         i = data.pop('id', None)
         c = globals().get('_color')
         util.display(str(i).rjust(indent-3), color='reset', style='bright') if i else None
         for k,v in data.items():
             if isinstance(v, unicode):
                 try:
                     j = json.loads(v.encode())
                     self._display(j, indent+2)
                 except:
                     util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                     util.display(str(v).encode(), color=c, style='dim')
             elif isinstance(v, list):
                 for i in v:
                     if isinstance(v, dict):
                         util.display(str(k).ljust(4  * indent).center(5 * indent))
                         self._display(v, indent+2)
                     else:
                         util.display(str(i).ljust(4  * indent).center(5 * indent))
             elif isinstance(v, dict):
                 util.display(str(k).ljust(4  * indent).center(5 * indent))
                 self._display(v, indent+1)
             elif isinstance(v, int):
                 if v in (0,1):
                     util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                     util.display(str(bool(v)).encode(), color=c, style='dim')
                 else:
                     util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                     util.display(str(v).encode(), color=c, style='dim')
             else:
                 util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                 util.display(str(v).encode(), color=c, style='dim')
     elif isinstance(data, list):
         for row in data:
             if isinstance(row, dict):
                 self._display(row, indent+2)
             else:
                 util.display(str(row).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                 util.display(str(v).encode(), color=c, style='dim')
     else:
         try:
             data = dict(data)
         except: pass
         if isinstance(data, collections.OrderedDict):
             data = dict(data)
         if isinstance(data, dict):
             i = data.pop('id',None)
             util.display(str(i).rjust(indent-1), color='reset', style='bright') if i else None
             self._display(data, indent+2)
         else:
             util.display(str(data.encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=','))
             util.display(v.encode(), color=c, style='dim')
示例#16
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-  
# by zhangzhi @2013-11-13 23:31:35 
# Copyright 2013 NONE rights reserved.
import util
import pca
import numpy as np
import sys

if __name__ == '__main__':
    for i in range(3):
        X = util.load_data(i, 5000)
        mean = np.mean(X, 0)

        evecs, evals = pca.pcaSvd(X)
        
        #test =  [ float(each) for each in evecs[0] ] 
        for j in range(20):
            if j == 0:
                #util.display(mean)
                pass
            util.display(evecs[j])

示例#17
0
文件: fepi.py 项目: snanurag/ml_fepi
def main():

    if len(sys.argv) > 1 and sys.argv[1] == "--debug":
        debug()

    configfile = ""
    caching = False
    if path.exists(os.getcwd() + "/config/config.yml"):
        configfile = os.getcwd() + "/config/config.yml"
    elif path.exists(os.getcwd() + "/config/config.yaml"):
        configfile = os.getcwd() + "/config/config.yaml"
    else:
        print("No config.yml file")

    with open(configfile, 'r') as stream:
        try:
            dict = yaml.load(stream)
            print("config file is parsed successfully.")
        except yaml.YAMLError as exc:
            print(exc)

    for k in dict:
        if k == 'cache':
            if dict.get(k) == True:
                caching = True
                break
            if dict.get(k) == False:
                if path.exists(parsing.cache_path):
                    shutil.rmtree(parsing.cache_path)

    for key, value in dict.items():
        if caching == True:
            if path.exists(parsing.cache_path) == False and key == 'cache':
                parsing.cache_data()
                caching = False
            elif path.exists(parsing.cache_path):
                if key == 'cache':
                    caching = False
                    parsing.read_from_cache()
                continue

        if key.startswith('cache'):
            parsing.cache_data()

        if key == "data":
            if 'read' in value.keys():
                data = parsing.read(value["read"])
            else:
                data = parsing.read_limited(value["read-limited"])
        if re.search("concat", key, re.IGNORECASE):
            manipulation.concat(value)
        if re.search("copy-data", key, re.IGNORECASE):
            parsing.copy(value)
        if re.search("csv", key, re.IGNORECASE):
            parsing.to_csv(value)
        if re.search("customize-cells", key, re.IGNORECASE):
            user_customization.customize(value)
        if re.search("customize-column", key, re.IGNORECASE):
            user_customization.customize_column(value)
        if re.search("customize-row", key, re.IGNORECASE):
            user_customization.customize_row(value)
        if re.search("delete-columns", key, re.IGNORECASE):
            manipulation.delete(value)
        if re.search("delete-df", key, re.IGNORECASE):
            parsing.delete_df(value)
        if re.search("delete-rows", key, re.IGNORECASE):
            manipulation.delete_row(value)
        if key.lower().startswith(("de-normalize")):
            alterations.denormalize(value)
        if re.search("display", key, re.IGNORECASE):
            util.display(value)
        if re.search("fillna-by-search", key, re.IGNORECASE):
            manipulation.fillna_by_search(value)
        elif re.search("fillna-by-mean", key, re.IGNORECASE):
            manipulation.fillna_by_mean(value)
        elif re.search("fillna", key, re.IGNORECASE):
            manipulation.fillna(value)
        if re.search("generate-column", key, re.IGNORECASE):
            user_customization.customize_column(value)
        if re.search("group-by", key, re.IGNORECASE):
            manipulation.group_by(value)
        if re.search("lightgbm", key, re.IGNORECASE):
            decision_tree.train(value)
        if key.lower().startswith("lstm"):
            lstm.train(value)
        if re.search("merge", key, re.IGNORECASE):
            manipulation.merge(value)
        if key.lower().startswith(("normalize-scaled")):
            alterations.normalize_scaled(value)
        elif key.lower().startswith(("normalize")):
            alterations.normalize(value)
        if re.search("ohe", key, re.IGNORECASE):
            manipulation.ohe(value)
        if re.search("partition", key, re.IGNORECASE):
            for l in value:
                for k1, v1 in l.items():
                    split_merge.input_partition(v1, k1)
        if key.lower().startswith(("matplot")):
            mat_plot_lib.plot(value)
        if re.search("dfs", key, re.IGNORECASE):
            for l in value:
                for k, v in l.items():
                    dfs.run_dfs(k, v)
        if re.search("keras", key, re.IGNORECASE):
            regression.train(value)
        if key.lower().startswith("script"):
            manipulation.script_run(value)
        if re.search("transfer", key, re.IGNORECASE):
            manipulation.transfer(value)
        if re.search('xgboost', key, re.IGNORECASE):
            xgboost_impl.train(value)
示例#18
0
    def _display(self, data, indent=4):
        c = globals().get('_color')

        if isinstance(data, dict):
            i = data.pop('id', None)
            util.display(str(i).rjust(indent-3), color='reset', style='bright') if i else None

            for k,v in data.items():
                if isinstance(v, unicode):
                    try:
                        j = json.loads(v.encode())
                        self._display(j, indent+2)
                    except:
                        util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                        util.display(str(v).encode().replace('\n',' ')[:40], color=c, style='dim')

                elif isinstance(v, list):
                    for i in v:
                        if isinstance(v, dict):
                            util.display(str(k).ljust(4  * indent).center(5 * indent))
                            self._display(v, indent+2)
                        else:
                            util.display(str(i).ljust(4  * indent).center(5 * indent))

                elif isinstance(v, dict):
                    util.display(str(k).ljust(4  * indent).center(5 * indent))
                    self._display(v, indent+1)

                elif isinstance(v, int):
                    if v in (0,1):
                        util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                        util.display(str(bool(v)).encode(), color=c, style='dim')
                    else:
                        util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                        util.display(str(v).encode(), color=c, style='dim')

                else:
                    util.display(str(k).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                    util.display(str(v).encode(), color=c, style='dim')

        elif isinstance(data, list):
            for row in data:
                if isinstance(row, dict):
                    self._display(row, indent+2)
                else:
                    util.display(str(row).encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=',')
                    util.display(str(v).encode(), color=c, style='dim')
        else:
            try:
                data = dict(data)
            except: pass

            if isinstance(data, collections.OrderedDict):
                data = dict(data)

            if isinstance(data, dict):
                i = data.pop('id',None)
                util.display(str(i).rjust(indent-1), color='reset', style='bright') if i else None
                self._display(data, indent+2)
            else:
                util.display(str(data.encode().ljust(4  * indent).center(5 * indent), color=c, style='bright', end=','))
                util.display(v.encode(), color=c, style='dim')
示例#19
0
文件: cnn1.py 项目: anlthms/meetup2
args = parser.parse_args()
train_idx, valid_idx = create_index_files(args.data_dir)

common_params = dict(sampling_freq=22050, clip_duration=16000, frame_duration=16)
train_params = AudioParams(**common_params)
valid_params = AudioParams(**common_params)
common = dict(target_size=1, nclasses=10, repo_dir=args.data_dir)
train = DataLoader(set_name='music-train', media_params=train_params,
                   index_file=train_idx, shuffle=True, **common)
valid = DataLoader(set_name='music-valid', media_params=valid_params,
                   index_file=valid_idx, shuffle=False, **common)
init = Gaussian(scale=0.01)
layers = [Conv((2, 2, 4), init=init, activation=Rectlin(),
               strides=dict(str_h=2, str_w=4)),
          Pooling(2, strides=2),
          Conv((3, 3, 4), init=init, batch_norm=True, activation=Rectlin(),
               strides=dict(str_h=1, str_w=2)),
          Affine(128, init=init, batch_norm=True, activation=Rectlin()),
          Affine(nout=common['nclasses'], init=init, activation=Softmax())]

model = Model(layers=layers)
opt = GradientDescentMomentum(learning_rate=0.01, momentum_coef=0.9)
metric = Misclassification()
callbacks = Callbacks(model, eval_set=valid, metric=metric, **args.callback_args)
cost = GeneralizedCost(costfunc=CrossEntropyMulti())

model.fit(train, optimizer=opt, num_epochs=args.epochs, cost=cost, callbacks=callbacks)
print('Misclassification error = %.1f%%' % (model.eval(valid, metric=metric)*100))
display(model, ['Convolution_0'], 'inputs')
display(model, ['Convolution_0', 'Convolution_1', 'Pooling_0'], 'outputs')
示例#20
0
文件: main.py 项目: jdenes/mogpl
while (not checkM):
    if (version_info[0] == 2): method = raw_input("CHOSE SOLVING METHOD (1: Dynamic Programming, 2: Linear Programming, 3: Mixed method):\n>> ")
    else: method = input("CHOSE SOLVING METHOD (1: Dynamic Programming, 2: Linear Programming, 3: Mixed method):\n>> ")
    if (method in ['1','2','3']): checkM = True
    else: print("Please chose 1, 2 or 3!\n")
while (not checkI):
    if (version_info[0] == 2): instance = raw_input("CHOSE INSTANCE TO SOLVE (an integer between 0 and 16):\n>> ")
    else: instance = input("CHOSE INSTANCE TO SOLVE (an integer between 0 and 16):\n>> ")
    if (instance in [str(x) for x in range(17)]): checkI = True
    else: print("Please chose an integer between 0 and 16!\n")

# Lancement de la résolution
print("\nSTARTING TO SOLVE INSTANCE %s USING %s..." %(instance, ["DYNAMIC PROGRAMMING", "LINEAR PROGRAMMING", "MIXED METHOD"][int(method) - 1]))
print("-------------------------------------------------------------------------------------------------------\n")

start = time()
constraints = createInstance(int(instance))
if (int(method) == 1): sol, finished = colorationDP(constraints)
if (int(method) == 2): sol, finished = colorationLP(constraints)
if (int(method) == 3): sol, finished = colorationMixed(constraints)
if (int(method) == 2 or int(method) == 3):
    print ("\n-------------------------------------------------------------------------------------------------------\n")

# Affichage des résultats
print("Execution time: %s sec " % (time() - start))
if (finished): print ("Grid coloration finished without error! Displaying solution...")
else: print ("Impossible to color the grid. Displaying coloration before error...")
display(sol, constraints, axis = False)

print("_______________________________________________________________________________________________________\n")
示例#21
0
def predict():
    # input from web
    state = str(request.form['state'])
    bus_name = str(request.form['restaurant'])
    star1 = str(request.form['star1'])
    star2 = str(request.form['star2'])
    star1, star2 = int(star1), int(star2)

    # selecting model to load depending on star selection from web app
    affix = None
    if star1 == 1 and star2 == 2:
        affix = "12"
    if star1 == 1 and star2 == 3:
        affix = "13"
    if star1 == 1 and star2 == 4:
        affix = "14"
    if star1 == 1 and star2 == 5:
        affix = "15"
    if star1 == 2 and star2 == 5:
        affix = "25"
    if star1 == 3 and star2 == 5:
        affix = "35"
    if star1 == 4 and star2 == 5:
        affix = "45"
    if affix:
        with open("../break_week/models/model" + affix + ".pickle", "rb") as f:
            models = pickle.load(f)

    # loading data from mongoDB using restaurant name and state
    db = client.yelp
    df = pd.DataFrame(
        list(db.review.find({
            "bus_name": bus_name,
            "state": state
        })))

    # calling display function
    df_pos, df_neg, name, size, Recall, Precision, Accuracy, lst_neg, \
                    lst_pos = display(models, df, star1, star2, state=state, bus_name=bus_name)

    # creating a list of count of words describing each aspect
    neg_asp_lst = list(df_neg["Aspect"])
    pos_asp_lst = list(df_pos["Aspect"])
    neg_words, pos_words = [], []
    for neg, pos in zip(df_neg["Level of experience"],
                        df_pos["Level of experience"]):
        neg_words.append(neg)
        pos_words.append(pos)

    # using created list to make a dict of aspects
    data_neg = {"Aspect": neg_asp_lst, "Level of experience": neg_words}
    data_pos = {"Aspect": pos_asp_lst, "Level of experience": pos_words}

    # using dict to make bar chart
    plot_neg = create_bar_chart(data_neg, "Negative customer experience",
                                "Aspect", "Level of experience")
    plot_pos = create_bar_chart(data_pos, "Positive customer experience",
                                "Aspect", "Level of experience")

    script_neg, div_neg = components(plot_neg)
    script_pos, div_pos = components(plot_pos)
    if len(lst_pos) >= 4 and len(lst_neg) >= 4:
        neg_asp1, neg_asp2, neg_asp3, neg_asp4, neg_asp5 = lst_neg[0][
            0], lst_neg[1][0], lst_neg[2][0], lst_neg[3][0], lst_neg[4][0]
        neg_desc1, neg_desc2, neg_desc3, neg_desc4, neg_desc5 = sort(
            lst_neg[0][1]), sort(lst_neg[1][1]), sort(lst_neg[2][1]), sort(
                lst_neg[3][1]), sort(lst_neg[4][1])
        pos_asp1, pos_asp2, pos_asp3, pos_asp4, pos_asp5 = lst_pos[0][
            0], lst_pos[1][0], lst_pos[2][0], lst_pos[3][0], lst_pos[4][0]
        pos_desc1, pos_desc2, pos_desc3, pos_desc4, pos_desc5 = sort(
            lst_pos[0][1]), sort(lst_pos[1][1]), sort(lst_pos[2][1]), sort(
                lst_pos[3][1]), sort(lst_pos[4][1])
    else:
        return render_template("index.html", title="Home")
    return render_template("predict.html",
                           bus_name=bus_name,
                           neg_the_div=div_neg,
                           neg_the_script=script_neg,
                           pos_the_div=div_pos,
                           pos_the_script=script_pos,
                           neg_asp1=neg_asp1,
                           neg_asp2=neg_asp2,
                           neg_asp3=neg_asp3,
                           neg_asp4=neg_asp4,
                           neg_asp5=neg_asp5,
                           neg_desc1=neg_desc1,
                           neg_desc2=neg_desc2,
                           neg_desc3=neg_desc3,
                           neg_desc4=neg_desc4,
                           neg_desc5=neg_desc5,
                           pos_asp1=pos_asp1,
                           pos_asp2=pos_asp2,
                           pos_asp3=pos_asp3,
                           pos_asp4=pos_asp4,
                           pos_asp5=pos_asp5,
                           pos_desc1=pos_desc1,
                           pos_desc2=pos_desc2,
                           pos_desc3=pos_desc3,
                           pos_desc4=pos_desc4,
                           pos_desc5=pos_desc5)
示例#22
0
 def show_sample(self):
     imgs = self.dataset.data[0:10].numpy()
     display(np.concatenate(imgs))