Ejemplo n.º 1
0
def lookup():
    if request.method == 'GET':
        return render_template('homepage.html')
    elif request.method == 'POST':
        ticker_symbol = request.form['ticker_symbol']
        data = Data()
        lookup_results = data.lookup(ticker_symbol)
        return render_template('dashboard.html', lookup_results=lookup_results)
Ejemplo n.º 2
0
def quote():
    if request.method == 'GET':
        return render_template('homepage.html')
    elif request.method == 'POST':
        ticker_symbol = request.form['ticker_symbol']
        trade_volume = int(request.form['trade_volume'])
        data = Data()
        quote_results = data.quote(ticker_symbol, trade_volume)
        return render_template('dashboard.html', quote_results=quote_results)
Ejemplo n.º 3
0
def test_gpp_derivative():
    datx = D.dalecData(1096)
    datdx = D.dalecData(1096)
    datdx.Cf = datdx.Cf*0.1
    lam = 0.00001
    mxdxgpp = M.GPP(datx.Cf+lam*datdx.Cf, datx, 0)
    mxgpp = M.GPP(datx.Cf, datx, 0)
    Mgpp = M.GPPdiff(datx.Cf, datx, 0)    
    print abs((mxdxgpp-mxgpp)/(Mgpp*lam*datdx.Cf)-1)
    assert abs(((mxdxgpp-mxgpp) / (Mgpp*lam*datdx.Cf))-1) < 1e-7
Ejemplo n.º 4
0
def dashboard():
    if request.method == 'GET':
        return render_template('homepage.html')
    elif request.method == 'POST':
        look_up_stock = request.form.get('look_up_stock')
        volume = request.form.get('volume')
        buy_stock = request.form.get('buy_stock')
        buy_volume = request.form.get('buy_volume')
        sell_stock = request.form.get('sell_stock')
        sell_volume = request.form.get('sell_volume')
        u = User(session["username"])
        users_balance = u.user_balance()
        users_orders_info = u.user_orders()
        users_holdings_info = u.user_holdings()
        data = Data()
        if volume:
            volume = int(volume)
            quote_results = data.quote(look_up_stock, volume)
            return render_template('dashboard.html',
                                   quote_results=quote_results,
                                   users_orders_info=users_orders_info,
                                   users_holdings_info=users_holdings_info,
                                   users_balance=users_balance)
        elif look_up_stock:
            lookup_results = data.lookup(look_up_stock)
            return render_template('dashboard.html',
                                   lookup_results=lookup_results,
                                   users_orders_info=users_orders_info,
                                   users_holdings_info=users_holdings_info,
                                   users_balance=users_balance)
        elif buy_stock:
            if u.buy(buy_stock, buy_volume):
                return render_template('dashboard.html',
                                       users_orders_info=users_orders_info,
                                       users_holdings_info=users_holdings_info,
                                       users_balance=users_balance)
                # This doesn't reload the page with latest data but the order goes through
            else:
                return render_template('error.html')
        elif sell_stock:
            if u.sell(sell_stock, sell_volume):
                return render_template('dashboard.html',
                                       users_orders_info=users_orders_info,
                                       users_holdings_info=users_holdings_info,
                                       users_balance=users_balance)
                # This doesn't reload the page with latest data but the order goes through
            else:
                return render_template('error.html')
        else:
            error_message = "Your order was not confirmed. Please try again."
            return render_template('dashboard.html',
                                   users_orders_info=users_orders_info,
                                   users_holdings_info=users_holdings_info,
                                   users_balance=users_balance,
                                   message=error_message)
Ejemplo n.º 5
0
 def POST(self):
     id = web.input().id
     value = web.input().value
     if not id or not value:
         return _response(200, 'no id or value')
     cfg = _getModel(web.ctx.ip)
     if not cfg.run:
         return _response(200, 'not run')
     for sc in cfg.cases:
         for scd in sc['data']:
             if scd['id'] == id:
                 if not scd['resp'] or scd['status'] != '4010':
                     _log.info('4010 redo')
                     return _response(200, 'null')
                 try:
                     scd['status'] = '4010 '
                     _updatedb(web.ctx.ip, cfg.to_json())
                     data = 'org=%s&taskName=%s&channel=hywechat&loginType=%s&passWord=%s' \
                            '&login=%s&extralParams=1001&extralParamsValue=%s&extralParamsA=' \
                            '&extralParamsValueA=&extralParamsB=&extralParamsValueB=&host=%s&port=%s' \
                            '&id=%s&timeout=0&signature=&taskey=%s&crawlChange=1000' % (
                                scd['org'], scd['entry'], scd['logintype'], scd['pwd'], scd['user'], value,
                                cfg.peerip, cfg.peerport, scd['resp']['id'], scd['resp']['taskkey'])
                     sit = _http_post(cfg.server[cfg.envs]['sendtaskurl'], data)
                     singleret = Data()
                     singleret.from_json(sit)
                     tmpp = Data()
                     tmpp.from_json(singleret.result)
                     singleret.result = tmpp
                     scd['resp'] = singleret
                 except:
                     return _response(500, 'single post error')
     _updatedb(web.ctx.ip, cfg.to_json())
     return _response(200, 'ok')
Ejemplo n.º 6
0
Archivo: knn.py Proyecto: zodman/cbr
def euclidean_distance(test_data, data_obj):
    distance = 0
    data_list = [data_obj.price,
                 data_obj.weekday(),
                 Data.is_light(data_obj.timestamp)
                 ]
    test_data = [test_data[1], 
                datetime.fromtimestamp(test_data[2]).weekday(),
                Data.is_light(test_data[2])
                ]
    for i, elem in enumerate(data_list):
        distance += pow((Decimal(test_data[i]) - elem), 2)
    return math.sqrt(distance)
Ejemplo n.º 7
0
def test_lin_model():
    datx = D.dalecData(1096)
    datdx = D.dalecData(1096)
    datxdx = D.dalecData(1096)
    lam = 0.0001
    datdx.Clist = lam*0.1*datdx.Clist
    datxdx.Clist = datxdx.Clist+lam*0.1*datxdx.Clist
    mxdxgpp = M.Clist(datxdx, 0, 10)
    mxgpp = M.Clist(datx, 0, 10)
    Matlist = M.Mlist(datx, 0, 10)
    Mgpp = M.Clist_lin(datdx, 0, 10, Matlist)    
    print abs(np.linalg.norm(mxdxgpp[10]-mxgpp[10]) /  \
              np.linalg.norm(Mgpp[10]) - 1)
    assert abs((np.linalg.norm(mxdxgpp[10]-mxgpp[10]) / \
                np.linalg.norm(Mgpp[10]))-1) < 1e-7
Ejemplo n.º 8
0
    def __init__(self, directory: str):
        self.data = []
        self._dir = directory

        file_names = sorted(fnmatch.filter(os.listdir(self._dir), '*.jpg'))
        for file_name in file_names:
            self.data.append(Data(self._dir, file_name.replace('.jpg', '')))
Ejemplo n.º 9
0
def interfaceCamera(port,dedicatedConnections,orchra,cloudHost, cloudPort):
    offload = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    offload.connect((cloudHost,cloudPort))
    print("camera are expected at port",port)
    s=createNewSocketConnection('0.0.0.0',port)
    while(True):
     c, addr = s.accept()
     while True:
      try:
        frameSize = c.recv(1024)
        imageLength= int(frameSize.decode("utf-8"))
        c.send((str('True')).encode("utf-8"))
        i=0
        chunk=b''
        while(i!=imageLength):
         imgByte= c.recv(1024)
         chunk=chunk+imgByte
         i=i+len(imgByte)
        if(orchra.atEdge):
         connectionToUse=orchra.resolution
         dataResult=sendForComputation(dedicatedConnections[orchra.resolution],frameSize,chunk)
        else:
         data=Data(orchra.resolution,chunk)
         stream=pickle.dumps(data)
         length=str(len(stream)).encode("utf-8")
         dataResult=sendForComputation(offload,length,stream)
        c.send("Helloworld".encode("utf-8"))
      except:
       break
Ejemplo n.º 10
0
def load() -> Tuple[Optional[Data], dict]:
    response = requests.post(constants.SYNC_ENDPOINT, _prepared_payload())
    content = response.json()
    try:
        data = Data(**content)
        return data, content
    except AttributeError:
        return None, content
Ejemplo n.º 11
0
def store(data):
    session = Session()
    if Data.compare_last(session, data):
        item = Data(**data)
        session.add(item)
        session.commit()
        logging.info('Stored!')
    else:
        logging.debug('No Changes')
Ejemplo n.º 12
0
 def create_data_entry(entry_type, date, title, subtitle, body):
     entry = Data(type=entry_type,
                  date=date,
                  title=title,
                  subtitle=subtitle,
                  body=body)
     session.add(entry)
     session.commit()
     return
Ejemplo n.º 13
0
Archivo: knn.py Proyecto: zodman/cbr
def get_neighbors(test_data, k=1):
    Distance.delete().execute()
    cat = test_data[0]
    datas = Data.select().where(Data.category == cat)
    for d in datas:
        dist = euclidean_distance(test_data, d)
        Distance.create(distance=dist, data=d)
    distances = Distance.select().order_by(Distance.distance.desc()).limit(k)
    return distances
Ejemplo n.º 14
0
    def _parse_block(self, user, block):
        msg_date = self._get_date(block)

        msg = self._get_msg(block)

        if not (msg_date and msg):
            return
        data = Data(user=user, date=msg_date, zone='safe')
        txt = []

        content = msg.text_content().strip().split('\n')

        for index, self.current_line in enumerate(content):
            self.current_line = self.current_line.strip()
            if not self.current_line:
                continue
            # Первая строка - локация
            elif not data.location:
                self._format_location_and_zone(data)
            # Если строка параметров
            elif self.PARAMS_REGEXP.match(self.current_line):
                self._format_km(data)
            # Если получили локацию, дальше должен идти км
            # Дополнительно проверяем, не является ли блок пропускаемым
            elif (data.location and not data.km) or self.check_skipped('all'):
                return
            # Проверка, не нужно ли пропустить строку
            elif self.check_skipped('line'):
                continue
            # Обработка полученного хлама
            elif self.current_line.startswith('Получено'):
                self._format_received(data)
            # Обработка полученных бонусов
            elif self.current_line.startswith('Бонус'):
                self._format_bonus(data)
            else:
                txt.append(self.current_line)
        # Если получили флаг пропустить блок, или не получили км, или не получили текстовку
        if not (data.km and txt):
            return
        txt = ' '.join(txt)
        data.txt_id = TXT_DICT_RELATION[txt]

        self.session.add(data)
Ejemplo n.º 15
0
def hello():
    if request.method == 'POST':
        data = ast.literal_eval(request.data)
        user_id = data['user_id']
        # 秒
        datetime_list = data['datetime'].split(',')
        longitude_list = data['longitude'].split(',')
        latitude_list = data['latitude'].split(',')
        pg_db.connect()
        pg_db.create_table(Data, True)
        for i in range(len(datetime_list)):
            Data.create(user_id=user_id,
                        datetime=datetime.fromtimestamp(int(datetime_list[i])),
                        longitude=float(longitude_list[i]),
                        latitude=float(latitude_list[i]))
            # print datetime_list[i], longitude_list[i], latitude_list[i]
        pg_db.close()
        return jsonify({'code': 0, 'msg': 'ok'})
    else:
        return jsonify({'code': 101, 'msg': 'method error!'})
Ejemplo n.º 16
0
def track(slug_name):
    if request.method == 'POST':
        track_id = session.query(Track).filter_by(
            slugname=slug_name).first().id
        ride = Data(driver=request.form['driver'],
                    time=request.form['time'].replace(',', '.'),
                    car=request.form['car'],
                    track_id=track_id,
                    added=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
        session.add(ride)
        session.commit()
    return show_track(slug_name)
Ejemplo n.º 17
0
 def POST(self):
     cfg = _getModel(web.ctx.ip)
     if cfg.run:
         return _response(400, u'测试中')
     cfg.run = True
     _log.info("starttest")
     cfg.sc_run = web.input().sc_run
     cfg.envs = web.input().envs
     org = web.input().org
     _log.info('sc_run:' + cfg.sc_run + ' envs:' + cfg.envs + ' org:' + org)
     if cfg.envs == '0':
         cfg.peerip = web.ctx.ip
         if web.ctx.ip == '127.0.0.1':
             cfg.peerip = '172.18.40.8'
         cfg.peerport = '8088'
     else:
         cfg.peerip = ''
         cfg.peerport = ''
     for testi in cfg.cases:
         for onetask in testi['data']:
             if org == 'all' or testi['bank'] == org:
                 data = 'org=%s&taskName=%s&channel=hywechat&loginType=%s&passWord=%s' \
                        '&login=%s&extralParams=1000&extralParamsValue=&extralParamsA=' \
                        '&extralParamsValueA=&extralParamsB=&extralParamsValueB=&host=%s&port=%s' \
                        '&id=&timeout=0&signature=&taskey=&crawlChange=1000' % (
                            onetask['org'], onetask['entry'], onetask['logintype'], onetask['pwd'], onetask['user'],
                            cfg.peerip,
                            cfg.peerport)
                 try:
                     it = _http_post(cfg.server[cfg.envs]['sendtaskurl'], data)
                     oneret = Data()
                     oneret.from_json(it)
                     tmpp = Data()
                     tmpp.from_json(oneret.result)
                     oneret.result = tmpp
                 except:
                     _log.error(traceback.format_exc())
                     _testshutdown(web.ctx.ip)
                     return _response(500, 'remote server error')
                 onetask['createtime'] = time.time()
                 onetask['status'] = '100'
                 onetask['rawdata'] = ''
                 onetask['resp'] = oneret
     _updatedb(web.ctx.ip, cfg.to_json())
     return _response(200, "success")
Ejemplo n.º 18
0
def dataprocess():

    with engine.connect() as connection:
        data = Data()
        data.create_tables(engine)
        data.excel_to_database(DATA_SOURCE,connection)
        data.optimize_tables(connection)

        connection.close()
        flash("Connection Closed!")

    return True
Ejemplo n.º 19
0
    def subscribe(self, identifer, callback: Callback, host: bool):

        # create session of not exist
        if identifer not in self.sessions:

            if host:
                # create session, it will be updated later by set
                self.set(identifer, Data("", 0, 0, State.Closed))
            else:
                # throw no session error
                callback.function(None, callback)
                return

        self.sessions[identifer].calbacks.append(callback)
Ejemplo n.º 20
0
def shownodeinfo():
    with engine.connect() as connection:
        data = Data()
        nodeinfo_out =[]
        if request.method == 'GET':
            if request.args["query"] == "nodeinfo":
                nodeinfo_out = data.query_one_node_information(connection,request.args['nid'])
                nodeinfo_out.update({"resulttype":"nodeinfo"})

            elif request.args["query"] == "price":
                nodeinfo_out = data.query_nodes_by_price(connection,request.args['minvalue'],request.args['maxvalue'])
                nodeinfo_out.update({"resulttype":"price"})

            elif request.args["query"] == "age":
                nodeinfo_out = data.query_nodes_by_age(connection,request.args['minvalue'],request.args['maxvalue'])
                nodeinfo_out.update({"resulttype":"age"})


            else:
                nodeinfo_out.update({"resulttype":"error"})
            #print nodeinfo_out

    return render_template('server/nodeinfo.html',nodeinfo=json.dumps(nodeinfo_out))
Ejemplo n.º 21
0
 def GET(self, id):
     if not id:
         return ''
     web.header('Content-Type', 'text/json; charset=utf-8', unique=True)
     cfg = _getModel(web.ctx.ip)
     for vc in cfg.cases:
         for vcd in vc['data']:
             if vcd['id'] == id:
                 try:
                     vi = Data()
                     if vcd.has_key('rawdata'):
                         vi.from_json(vcd['rawdata'])
                         if len(vi.result) == 0 and isinstance(vcd['resp'], types.DictType):
                             vi.from_dict(vcd['resp'])
                     else:
                         vi.from_dict(vcd['resp'])
                     return vi.to_json()
                 except:
                     return vcd['resp']
     return ''
def get_all_viajes():
    if request.method == 'POST':
        new_data = Data(request.json['firstName'], request.json['lastName'],
                        request.json['emailAddress'],
                        int(request.json['cardNumber']),
                        int(request.json['pointsQty']),
                        int(request.json['milesQty']),
                        request.json['originCity'],
                        request.json['destinationCity'],
                        request.json['startDate'], request.json['endDate'])
        db.session.add(new_data)
        db.session.commit()
        return 'registro insertado exitosamente'
    else:
        return jsonify({'viajes': viajes})
Ejemplo n.º 23
0
def main():
    data = Data.select_by_sql("SELECT * FROM data")

    limpiar = ['Idioma','País', 'formatos']
    for dato in data:
        sujeto = dato.sujeto
        predicado = dato.predicado
        objeto = dato.objeto
        if predicado in limpiar:
            limpiador(sujeto, predicado, objeto)
        else:
            if predicado == "Materias":
                review(sujeto, predicado, objeto)
            else:
                add_data(sujeto, predicado, objeto)
Ejemplo n.º 24
0
 async def data_save(self, data):
     data = str(data, encoding="utf-8")
     print(data)
     data1, data2, data3 = data.split("-")
     data1 = round(float(data1), 2)
     data2 = round((1 - float(data2) / 6000) * 100, 2)
     data3 = round(float(data3), 2)
     data_set = {'N': [data1 + 100], 'P': [data2 + 100], 'K': [data3 + 100]}
     x = pd.DataFrame(data_set)
     output = self.svm_clf.predict(x)[0]
     level = self.setlevel(output)
     d = Data(cid=3,
              illumination=data3,
              humidity=data2,
              temperature=data1,
              level=level,
              arrivetime=time.strftime("%Y-%m-%d %H:%M:%S",
                                       time.localtime()))
     await d.save()
Ejemplo n.º 25
0
    def run(self):
        while not self.event.is_set():
            
            cpu = psutil.cpu_percent()
            mem = psutil.virtual_memory().available
            disk = psutil.disk_usage('/').free
            t = int(time.time())
            data = Data(event_time=t, cpu_usage=cpu, free_ram=mem, free_disk=disk)
            db.session.add(data)
            db.session.commit()


            _count = Data.query.count()
            if _count > 100:
                deletable = Data.query.order_by(Data.event_time).first()
                db.session.delete(deletable)
                db.session.commit()

            self.event.wait(self.seconds)
Ejemplo n.º 26
0
def jsonprocess():

    with engine.connect() as connection:
        data = Data()

        #get json file for node tree
        raw_data = data.query_node_information(connection)
        jsontree = Jsontree()
        jsontree.get_jsonfile_tree_node(raw_data)
        flash("get json file for node tree")

        # get json file for equipment ddtree
        raw_data = data.query_equipment_cost(connection)
        jsontree = Jsontree()
        jsontree.get_jsonfile_ddtree_equipment(raw_data)
        flash("get json file for equipment ddtree")

        #get json file for equipment tree
        node_data = data.query_equipment(connection)
        jsontree = Jsontree()
        jsontree.get_jsonfile_tree_equipment(node_data)


        #get json file fro node infomation
        raw_data = data.query_node_information(connection)
        jsondata= Jsontree()
        jsondata.get_jsonfile_parallel(raw_data)
        flash("get json file fro node infomation")

        #get json file for forcegraph
        raw_data =  data.query_equipment(connection)
        jsondata= Jsontree()
        jsondata.get_jsonfile_forcegraph(raw_data)

        #get json file for nodeforcegraph
        raw_data = data.query_node_information(connection)
        jsontree = Jsontree()
        jsontree.get_jsonfile_nodeforcegraph(raw_data)
        flash("get json file for nodeforcegraph")

        connection.close()
        flash("Connection Closed!")

    return True
def query(dataset_dir: str, query_dir: str, methods: List[AbstractMethod]):
    data = Data(dataset_dir)
    file_names = fnmatch.filter(os.listdir(query_dir), '*.jpg')

    print('Training...')
    texts_recs = [method.train(data.pictures) for method in methods]

    print('Querying...')

    query_pictures = seq(file_names).map(lambda query_name: Picture(query_dir, query_name)).to_list()

    results = []
    for method in methods:
        print('\tRunning method', method.__class__.__name__)
        mres = []
        for picture in tqdm(query_pictures, file=sys.stdout):
            mres.append((picture,) + get_result(method, picture))
        results.append(mres)

    return results, texts_recs
Ejemplo n.º 28
0
def experiment(data_dir):
    data = Data.load(data_dir)
    data.v = data.v[:, sum(data.v > 0) > 0]

    model = Model.default_model(1e-5, data.v.shape[1], 0)

    print('filtering...')
    result = model.filter(data.v)
    result.evaluate(data.s)
    print('\tF-score : ' + str(result.score))

    print('smoothing...')
    result = model.smooth(data.v)
    result.evaluate(data.s)
    print('\tF-score : ' + str(result.score))

    print('online smoothing...')
    result = model.online_smooth(data.v, lag=10)
    result.evaluate(data.s)
    print('\tF-score : ' + str(result.score))
Ejemplo n.º 29
0
def callbackFunction(data: Data, callback: Callback):

    if type(callback.payload) is socket.socket:
        client: socket = callback.payload

        # if no data (subscribe failed)
        if data is None:
            send_msg(
                client,
                json.dumps({
                    'status': 'error',
                    'description': 'Session not found',
                    'code': '9'
                }))
            return

        # else send data
        try:
            msg = json.dumps({'status': 'ok', 'new_data': data.dictValue()})
            send_msg(client, msg)
        except:
            manager.unsubscribe(callback)
Ejemplo n.º 30
0
def visualize_data(dirname, m, n):
    data = Data.load(dirname)
    v = data.v.transpose()
    t = v.shape[1]
    print(t)
    if m > 0:
        fig = plt.figure(figsize=(12, 4))
        ax = fig.gca()
        ax.pcolormesh(v[0:m, :], cmap=plt.cm.Greys)
        ax.vlines(np.arange(0, t), 0, data.s * m, colors='r', linestyles='-', linewidth=2)
        ax.legend(['change points'])
    if n > 0:
        fig = plt.figure(figsize=(12, 4))
        gs = gridspec.GridSpec(n, 1, height_ratios=np.ones(n))
        for i in range(n):
            ax = plt.subplot(gs[i])
            y = v[m + i, :]
            y_lim_max = np.max(y) * 1.1
            ax.plot(range(t), y, 'b-')
            ax.vlines(np.arange(0, t), 0, data.s * y_lim_max, colors='r', linestyles='-', linewidth=2)
            ax.set_ylim([0, y_lim_max])
    plt.show()
Ejemplo n.º 31
0
def adminlogin():
    if request.method == 'GET':
        return render_template('adminlogin.html')
    elif request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        adminkey = request.form['adminkey']
        d = Data()
        users = d.users()
        holdings = d.holdings()
        orders = d.orders()
        leaderboard = d.leaderboard()
        u = User(username)
        if u.admin(password, adminkey):
            return render_template('admindashboard.html',
                                   users=users,
                                   holdings=holdings,
                                   orders=orders,
                                   leaderboard=leaderboard)
        else:
            return render_template('adminlogin.html')
Ejemplo n.º 32
0
def superuser():
    if request.method == 'GET':
        return render_template('secretlogin.html')
    elif request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        superkey = request.form['superkey']
        d = Data()
        users = d.users()
        holdings = d.holdings()
        orders = d.orders()
        leaderboard = d.leaderboard()
        super_balance = 10000000000000
        u = User(username)
        if u.superuser(password, superkey):
            return render_template('superuser.html',
                                   users=users,
                                   holdings=holdings,
                                   orders=orders,
                                   leaderboard=leaderboard,
                                   super_balance=super_balance)
        else:
            return render_template('secretlogin.html')
Ejemplo n.º 33
0
def load_data(file_path, teachers_empty_space, groups_empty_space,
              subjects_order):
    """
    Loads and processes input data, initialises helper structures.
    :param file_path: path to file with input data
    :param teachers_empty_space: dictionary where key = name of the teacher, values = list of rows where it is in
    :param groups_empty_space: dictionary where key = group index, values = list of rows where it is in
    :param subjects_order: dictionary where key = (name of the subject, index of the group), value = [int, int, int]
    where ints represent start times (row in matrix) for types of classes P, V and L respectively. If start time is -1
    it means that that subject does not have that type of class.
    :return: Data(groups, teachers, classes, classrooms)
    """
    with open(file_path) as file:
        data = json.load(file)

    # classes: dictionary where key = index of a class, value = class
    classes = {}
    # classrooms: dictionary where key = index, value = classroom name
    classrooms = {}
    # teachers: dictionary where key = teachers' name, value = index
    teachers = {}
    # groups: dictionary where key = name of the group, value = index
    groups = {}
    class_list = []

    for cl in data['Time']:
        new_group = cl['Group']
        new_teacher = cl['Teacher']

        # initialise for empty space of teachers
        if new_teacher not in teachers_empty_space:
            teachers_empty_space[new_teacher] = []

        new = Class(new_group, new_teacher, cl['Subject'], cl['Type'],
                    cl['Duration'], cl['Classroom'])
        # add groups
        for group in new_group:
            if group not in groups:
                groups[group] = len(groups)
                # initialise for empty space of groups
                groups_empty_space[groups[group]] = []

        # add teacher
        if new_teacher not in teachers:
            teachers[new_teacher] = len(teachers)
        class_list.append(new)

    # shuffle mostly because of teachers
    random.shuffle(class_list)
    # add classrooms
    for cl in class_list:
        classes[len(classes)] = cl

    # every class is assigned a list of classrooms he can be in as indexes (later columns of matrix)
    for type in data['Classrooms']:
        for name in data['Classrooms'][type]:
            new = Classroom(name, type)
            classrooms[len(classrooms)] = new

    # every class has a list of groups marked by its index, same for classrooms
    for i in classes:
        cl = classes[i]

        classroom = cl.classrooms
        index_classrooms = []
        # add classrooms
        for index, c in classrooms.items():
            if c.type == classroom:
                index_classrooms.append(index)
        cl.classrooms = index_classrooms

        class_groups = cl.groups
        index_groups = []
        for name, index in groups.items():
            if name in class_groups:
                # initialise order of subjects
                if (cl.subject, index) not in subjects_order:
                    subjects_order[(cl.subject, index)] = [-1, -1, -1]
                index_groups.append(index)
        cl.groups = index_groups

    return Data(groups, teachers, classes, classrooms)
Ejemplo n.º 34
0
import os
BASE = os.path.abspath(os.path.dirname(__file__))

## learning rate of 0.4
## normalization
ADDMISSIONS_DATASET = os.path.join(
    BASE, 'example_datasets',
    'Admission_Predict.csv')  ## learning rate of 0.4 is good

## learning rate of 0.03
## no normalization
CRIME_RATE_DATASET = os.path.join(BASE, 'example_datasets',
                                  'Crime_normalized.csv')

if __name__ == '__main__':
    d = Data(ADDMISSIONS_DATASET)
    x = d(1, 2)
    y = d(d.size[1])

    x = Matrix(x)
    y = Vector(y)

    x_norm = Model.normalize(x)
    y_norm = Model.normalize(y)

    model = Model(x_norm, y, learning_rate=0.4)

    print('INITIAL WEIGHTS : ')
    print(model.weights)

    # learning
Ejemplo n.º 35
0
def on_new_client(clientsocket, addr):

    logger.info('Client connected: ' + str(addr))

    identifer = None  #session identifer
    token = None  #access token
    subscribeCallback = None
    disconnect = False

    while not disconnect:

        msg = recv_msg(clientsocket)

        # Check connection
        if (msg is None or len(msg) == 0):
            break

        coomnds = msg.split('<EOF>')
        for command in coomnds:

            # skip empty command
            if (len(command) == 0):
                continue

            # Parse
            try:
                responce = json.loads(msg)
            except ValueError:
                send_msg(
                    clientsocket,
                    json.dumps({
                        'status': 'error',
                        'description': 'Not valid JSON',
                        'code': '1'
                    }))
                disconnect = True
                break  #disconnect user

            # Access variables
            if 'token' in responce:
                token = responce['token']

            if 'identifer' in responce:
                identifer = responce['identifer']

            if token is None:
                send_msg(
                    clientsocket,
                    json.dumps({
                        'status': 'error',
                        'description': 'No token',
                        'code': '2'
                    }))
                disconnect = True
                break  #disconnect user

            if token != SECRET_TOKEN:
                send_msg(
                    clientsocket,
                    json.dumps({
                        'status': 'error',
                        'description': 'Wrong token',
                        'code': '4'
                    }))
                disconnect = True
                break  #disconnect user

            if identifer is None:
                send_msg(
                    clientsocket,
                    json.dumps({
                        'status': 'error',
                        'description': 'No identifer',
                        'code': '3'
                    }))
                continue

            # Commands
            if 'command' in responce:
                command = responce['command']

                # Get info
                if (command == 'get'):
                    logger.info('Client ' + str(addr) + " get ifo about " +
                                identifer)
                    data = manager.get(identifer)
                    if data is None:
                        send_msg(
                            clientsocket,
                            json.dumps({
                                'status': 'error',
                                'description': 'Session not found',
                                'code': '9'
                            }))
                    else:
                        send_msg(
                            clientsocket,
                            json.dumps({
                                'status': 'ok',
                                'new_data': data.dictValue()
                            }))

                # Subscribe
                if (command == 'subscribe' or command == 'host'):

                    #remove old callback
                    if (subscribeCallback is not None):
                        manager.unsubscribe(identifer, subscribeCallback)

                    subscribeCallback = Callback(callbackFunction,
                                                 clientsocket)
                    manager.subscribe(identifer, subscribeCallback,
                                      (command == 'host'))
                    logger.info('Client ' + str(addr) + " subscribed " +
                                identifer)
                    send_msg(clientsocket,
                             json.dumps({
                                 'status': 'ok',
                                 'code': '0'
                             }))

                # Set info
                if (command == 'set'):

                    if 'file' in responce:
                        file = responce['file']
                    else:
                        send_msg(
                            clientsocket,
                            json.dumps({
                                'status': 'error',
                                'description': 'No file',
                                'code': '5'
                            }))
                        continue

                    if 'duration' in responce:
                        duration = float(responce['duration'])
                    else:
                        send_msg(
                            clientsocket,
                            json.dumps({
                                'status': 'error',
                                'description': 'No duration',
                                'code': '6'
                            }))
                        continue

                    if 'position' in responce:
                        position = float(responce['position'])
                    else:
                        send_msg(
                            clientsocket,
                            json.dumps({
                                'status': 'error',
                                'description': 'No position',
                                'code': '7'
                            }))
                        continue

                    if 'state' in responce:
                        state = int(responce['state'])
                    else:
                        send_msg(
                            clientsocket,
                            json.dumps({
                                'status': 'error',
                                'description': 'No state',
                                'code': '8'
                            }))
                        continue

                    logger.info('Client ' + str(addr) + " set info " +
                                identifer)
                    manager.set(identifer, Data(file, duration, position,
                                                state))
                    send_msg(clientsocket,
                             json.dumps({
                                 'status': 'ok',
                                 'code': '0'
                             }))

    logger.info('Client disconnected: ' + str(addr))

    # Force unsubscribe
    if (subscribeCallback is not None):
        manager.unsubscribe(identifer, subscribeCallback)

    clientsocket.close()
Ejemplo n.º 36
0
def add_data(sub, pred, obj):
    try:
        Data(sujeto=sub.strip(), predicado=pred.strip(), objeto=obj.strip())
    except Exception as e:
        print e
        pass
Ejemplo n.º 37
0
import numpy as np
import sys
sys.path.append('../../../')
from logistic_regression import LogisticRegression
from model import Data
from utilities import *

train_data = np.load('data/train_data.npy')
test_data = np.load('data/test_data.npy')

train_inputs, train_target = Data.normalize(
    train_data[:, :-1]), train_data[:, -1:].astype(int).flatten()
test_inputs, test_target = Data.normalize(
    test_data[:, :-1]), test_data[:, -1:].astype(int).flatten()

model = LogisticRegression(input_dim=7,
                           num_classes=3,
                           batch_size=8,
                           epochs=50,
                           learning_rate=1e-3)
model.train(train_inputs, train_target)

print('After training the model accuracy is about ',
      accuracy(model.predict(test_inputs), test_target))
confusion_plot(model,
               test_inputs,
               test_target,
               outfile='plots/confusion_matrix')
Ejemplo n.º 38
0
def test():
    engine = ConnectDatabase().connect_sqlalchemy()
    connection = engine.connect()
    data = Data()
    raw_data = data.query_equipment_cost(connection)
    return "Test data"
Ejemplo n.º 39
0
        outputs = model(inputs.cuda())
        loss = criterion(outputs, targets.cuda())

    scaler.scale(loss).backward()
    scaler.step(optimizer)
    scaler.update()

    return loss.item()


trainer = Engine(update)
#trainer = create_supervised_trainer(model, optimizer, criterion, device='cuda')
val_metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)}
evaluator = create_supervised_evaluator(model, metrics=val_metrics, device='cuda')

data = Data(x=X,
            y=y)

loader = DataLoader(dataset=data,
                    shuffle=True,
                    batch_size=args.batch_size)


@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(trainer):
    evaluator.run(data=loader)
    metrics = evaluator.state.metrics
    print("Training Results - Epoch: {}  Avg accuracy: {:.2f} Avg loss: {:.2f}"
          .format(trainer.state.epoch, metrics["accuracy"], metrics["loss"]))


trainer.add_event_handler(Events.ITERATION_COMPLETED, scheduler)
Ejemplo n.º 40
0
def test_nee():
    dC=D.dalecData(1095)
    assert M.NEE(0,0,0,0,0,0,dC) == 0
Ejemplo n.º 41
0
def add_data(sensor, data):
    s = Session()
    s.add(Data(sensor, data))
    s.commit()