Exemplo n.º 1
0
 def test_update_as_dict(self):
     data = Data()
     data.__dict__.update({
         "range": "1",
         "number_of_primes": 1,
         "duration": 1.0
     })
     self.assertEqual("1;1;1.0", data.__str__())
Exemplo n.º 2
0
    def setUp(self):
        self.alg = System(self.sc, config=StubConfig)
        self.data = Data(self.sc, config=StubConfig)
        self.maximum_weight = StubConfig.get('System', 'maximum_weight')

        # Replace all other recommenders with a
        # stub that likes every restaurant
        # This stub must be defined in default.cfg
        self.alg.recommenders = {'CuisineType': StubCuisineType(self.sc)}
        self.alg.weights = {'CuisineType': 1}
Exemplo n.º 3
0
 def revert_password():
     user_url = Data.BASE_API_PATH + '/user/password/change'
     json = Data.json_for_update_password(Data.read_credentials()[1],
                                          Data.read_credentials()[2])
     response = requests.post(
         user_url,
         headers=Authorization().authentification_with_new_password(),
         json=json)
     status_code = response.status_code
     return status_code
Exemplo n.º 4
0
    def run(self, c=5, param=100, n_fold_cv=10, kernel='RBF'):
        '''
        Run tests with given parameters on currently loaded data
        @param c: SVM classifier parameter
        @param param: kernel parameter
        @param n_fold_cv: n-fold cross-validation parameter
        @param kernel: used kernel - possibilities in src/kernels.py
        @return: classification results
        '''
        data = Data(dbfile=None, n_fold_cv=n_fold_cv)

        k = str2kernel[kernel](param=param)
        C = c
        self.svm = SVM(kernel=k, C=C)

        correct_sum = 0
        test_len = 0
        res = {}
        res['true_positive'] = 0.0
        res['true_negative'] = 0.0
        res['false_positive'] = 0.0
        res['false_negative'] = 0.0
        for i in xrange(n_fold_cv):
            X1, Y1, X2, Y2 = data.get(i)
            self._logger.info('Training SVM... (i={0})'.format(str(i)))
            self.svm.train(X1, Y1)
            if self.svm.model_exists:
                # predict
                Y_predict = self.svm.predict(X2)
                self._logger.info('using {0} of {1} support vectors'.format(
                    self.svm.lm_count, self.svm.all_lm_count))

                # calculate tp, fp, tn, fn
                test_len = len(Y_predict)
                Y_predict_P = Y_predict[:(test_len/2)]
                Y_predict_N = Y_predict[(test_len/2):]
                Y2_P = Y2[:(test_len/2)]
                Y2_N = Y2[(test_len/2):]
                tp = np.sum(Y_predict_P == Y2_P)
                fn = np.sum(Y_predict_P != Y2_P)
                tn = np.sum(Y_predict_N == Y2_N)
                fp = np.sum(Y_predict_N != Y2_N)
                res['true_positive'] += tp / float(n_fold_cv)
                res['false_positive'] += fp / float(n_fold_cv)
                res['true_negative'] += tn / float(n_fold_cv)
                res['false_negative'] += fn / float(n_fold_cv)

                # this iteration result
                self._logger.info('tp: {0}, fp: {1}, tn: {2}, fn :{3}'.format(
                    str(tp), str(fp), str(tn), str(fn)))

        # print and return results
        self._calculate_results(res)
        return res
Exemplo n.º 5
0
 def regenerate_data(self, dbfile, count=1000, max_token_size=1):
     '''
     Regenerate database files according to input parameters
     @param dbfile: database file containing anotated data
     @param count: used number of entries
     @param max_token_size: sets maximal size of word tokens
     @return: data transformed into SVM usable format
     '''
     data = Data(dbfile=dbfile, max_token_size=max_token_size)
     data.regenerate_X1_X2(count)
     return data
Exemplo n.º 6
0
def process_tests_text(bot: TeleBot, user: User, data: Data, text: str,
                       user_func: Callable):
    test_action = text.split("__")[1]

    if test_action == "help":
        bot.send_message(user.chat_id, help_msg)

    elif test_action.startswith("edit"):
        btn_number = int(text.split("-")[-1])

    elif test_action == "update":
        data.update_ejf_table()
        bot.send_message(user.chat_id, text="EJF table has been updated")

    elif test_action == "resetquiz":
        user.additional_info = None
        user.save()
        bot.send_message(
            user.chat_id,
            text="You can now click /start and take a quiz again.")

    elif test_action == "user":
        user_func(user)

    elif test_action == "test_blocked":

        blocked_counter = 0
        unblocked_counter = 0
        for u in User.objects:
            try:
                m = bot.send_message(chat_id=u.chat_id, text="check")
                bot.delete_message(chat_id=u.chat_id, message_id=m.message_id)
                if u.is_blocked:
                    u.is_blocked = False
                    bot.send_message(
                        user.chat_id,
                        text=f"{u.chat_id} @{u.username} unblocked!!!!")
                    u.save()
                    unblocked_counter += 1

            except Exception as e:
                bot.send_message(user.chat_id,
                                 text=f"{u.chat_id} @{u.username} {e}")
                if u.is_blocked is False:
                    u.is_blocked = True
                    u.save()
                    blocked_counter += 1

        bot.send_message(
            user.chat_id,
            text=
            f"Заблоковано нових {blocked_counter}\nРозблоковано нових {unblocked_counter}",
        )
Exemplo n.º 7
0
    def parse_dat_file(self):
        self.dat_dict = {}

        count = 0
        for fname in self.all_file_list:
            count += 1
            print("{}/{} Reading... {}".format(count, len(self.all_file_list),
                                               fname))
            data = Data()
            dat = parse_dat(fname)
            self.dat_dict[fname] = dat
            data.dat_data = dat
            self.data_dict[fname] = data
Exemplo n.º 8
0
def trainmodel():
    fvocab = "data/sample/vocab.pickle.gz"
    fdata = "data/sample/trn.data"
    flabel = "data/sample/trn.label"
    D = load(gzip.open(fvocab))
    vocab, labelidxmap = D['vocab'], D['labelidxmap']
    print('len(vocab) = {}'.format(len(vocab)))
    data = Data()
    trnM, trnL = data.loadmatrix(fdata, flabel)
    print('trnM.shape = {}'.format(trnM.shape))
    idxlabelmap = reversedict(labelidxmap)
    pm = ParsingModel(vocab=vocab, idxlabelmap=idxlabelmap)
    pm.train(trnM, trnL)
    pm.savemodel("models/parsing-model.pickle.gz")
Exemplo n.º 9
0
 def change_password(self):
     content = []
     user_url = Data.BASE_API_PATH + '/user/password/change'
     json = Data.json_for_update_password(Data.read_credentials()[2],
                                          Data.read_credentials()[1])
     response = requests.post(user_url,
                              headers=self.user_access_token,
                              json=json)
     status_code = response.status_code
     content.append(status_code)
     response = response.json()
     content.append(response)
     content.append(Data.json_for_create_user())
     return content
Exemplo n.º 10
0
 def update_project(self, project):
     project_url = Data.BASE_API_PATH + 'project/' + project
     response = requests.put(project_url,
                             headers=self.admin_token,
                             json=Data.json_for_update_project())
     content = ApiHelper.content(response)
     return content
Exemplo n.º 11
0
class RangeOfNumbers:
    data = Data()
    _increment = 1 * 1000 * 1000

    @staticmethod
    def get_increment():
        return RangeOfNumbers._increment

    def __init__(self, start: int = 0, final: int = None):
        self.start = start
        self.final = final if final else start + self.get_increment()
        self.file = f'{self.start}-{self.final}'

    def calculate(self):
        a = time.time()
        self.create_files()
        b = time.time()
        self.data.__dict__.update({'duration': round(b - a, 5)})
        export_information(self.data)

    def create_files(self):
        if not os.path.exists(path_files):
            os.mkdir(path_files)
        range_start, range_end = self.file.split('-')
        list = prime_list(int(range_start), int(range_end))
        write_primes(f"primos-{range_start}-{range_end}.txt", list)

        self.data.__dict__.update({
            'range': self.file,
            'number_of_primes': len(list)
        })

    def __str__(self):
        return f'start: {self.start}, end: {self.final}, file: {self.file}'
Exemplo n.º 12
0
class TestData:

    data = {
        'device': '1',
        'seqNumber': 0,
        'data': {
            'temperature': 10,
            'latitude': 38,
            'longitude': 20
        },
        'time': '2019-10-10 17:00:00'
    }

    dataR = Data()

    def test_received(self):

        rec = self.dataR.received(json.dumps(self.data))
        assert self.data.get('device') == rec.get('device')
        assert self.data.get('seqNumber') == rec.get('seqNumber')
        assert self.data.get('data').get('temperature') == rec.get('data').get(
            'temperature')

    def test_get_data(self):

        rec = self.dataR.received(json.dumps(self.data))
        getted = self.dataR.get_data()
        assert getted.get('device') == rec.get('device')
        assert getted.get('seqNumber') == rec.get('seqNumber')
        assert getted.get('data').get('temperature') == rec.get('data').get(
            'temperature')
Exemplo n.º 13
0
def read_information():
    import datetime
    """
    Read information from csv file
    :return: Information
    """
    dat: [Data] = []
    with open(f'{csv_file}', "r") as file:
        lines = file.readlines()

    for l in lines[1:]:
        dat.append(
            Data(
                l.split(';')[0], int(l.split(';')[1]), float(l.split(';')[2])))

    primos = 0
    tiempo = 0

    for d in dat:
        primos += d.number_of_primes
        tiempo += d.duration

    info = {
        'rangos': len(dat),
        'primos': primos,
        'tiempo': str(datetime.timedelta(seconds=tiempo))
    }
    print(info)
Exemplo n.º 14
0
    def openDialog(self):

        # Only single instance of Plugin
        if self.exists == True: return
        else: self.exists = True

        self.resources = Resources(self.plugin_dir)
        self.dlg = PlanHeatDMMDialog(self)
        self.data = Data(plugin=False)

        splash = QSplashScreen(
            QtGui.QPixmap(self.plugin_dir + os.path.sep +
                          'resources/PlanHeatPrincipal.png'),
            QtCore.Qt.WindowStaysOnTopHint)
        splash.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint
                              | QtCore.Qt.FramelessWindowHint)
        splash.setEnabled(False)
        splash.show()

        # Run the dialog event loop
        self.run()
        splash.finish(self.dlg)
        self.dlg.show()

        result = self.dlg.exec_()

        self.exists = False

        # See if OK was pressed
        if result:
            # Do something useful here
            pass
        else:
            pass
Exemplo n.º 15
0
    def resolve_route(project_route, page_route, doc_route):
        project = Data.get_project(project_route)
        if project is None:
            return Routing.show_projects_page()

        if project.has_pages() is False:
            logging.error('Project ' + project.display_name + ' has no pages')
            return Routing.show_projects_page()

        # Try to get page by route if it is defined
        # otherwise get the first page.
        page = project.get_page(page_route)
        if page == None:
            return Routing.show_projects_page()

        # If the page has no documents, try to fetch first page with documents
        # If not, redirect to project page.
        if page.has_documents() is False:
            page = project.get_page_with_docs()

        if page == None:
            return Routing.show_projects_page()

        doc = page.get_document(doc_route)
        if doc == None:
            return Routing.show_projects_page()

        return render_template('index.html',
                               base_href=Routing.base_href,
                               selected_page=page,
                               page=page,
                               doc_route=doc.route,
                               docs=page.documents,
                               html_content=doc.to_html(),
                               project=project)
Exemplo n.º 16
0
 def upload_photo(self, photo):
     data_url = Data.BASE_API_PATH + '/data/photo'
     response = requests.post(data_url,
                              headers=self.user_access_token,
                              files=Data.return_photo(photo))
     content = ApiHelper.content(response)
     return content
Exemplo n.º 17
0
    def run(self):
        """Run method that performs all the real work"""
        try:
            # locale.setlocale(locale.LC_ALL, 'en-GB')
            # Only single instance of Plugin
            if self.exists == True:
                return
            else:
                self.exists = True

            self.dlg = PlanHeatDMMDialog(self)
            self.resources = Resources(self.plugin_dir)
            self.data = Data(plugin=True)

            # Name of project
            self.data.projectName = master_mapping_config.CURRENT_PROJECT_NAME

            # Redirection
            dmm_folder = os.path.join(master_mapping_config.CURRENT_MAPPING_DIRECTORY,
                                                    master_mapping_config.DMM_FOLDER)
            os.makedirs(dmm_folder, exist_ok=True)
            self.data.outputSaveFile = os.path.join(dmm_folder, master_mapping_config.DMM_PREFIX)


            splash = QSplashScreen(QtGui.QPixmap(self.plugin_dir + os.path.sep + 'resources/PlanHeatPrincipal.png'), QtCore.Qt.WindowStaysOnTopHint)
            splash.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint)
            splash.setEnabled(False)
            splash.show()

            # Run the dialog event loop
            initWindowStatus(self)
            self.resources.loadAppResources()
            initWindowbehavior(self)

            # Deserialize the module
            DMMSerializer.deserialize(self)

            splash.finish(self.dlg)

            # show the dialog
            self.dlg.show()

            # Run the dialog event loop
            result = self.dlg.exec_()

            self.exists = False

            # See if OK was pressed
            if result:
                # Do something useful here -
                pass
            else:
                pass



        except Exception as e:
            self.exists = False
            print(str(e))
Exemplo n.º 18
0
 def assign_user_on_project(self, project):
     project_url = Data.BASE_API_PATH + 'project/' + project + '/assign'
     response = requests.put(
         project_url,
         headers=self.admin_token,
         json=Data.json_for_assign_user('user_for_assignee'))
     content = ApiHelper.content(response)
     return content
Exemplo n.º 19
0
 def create_new_dashboard(self):
     project = ApiHelper.get_project_name(self)
     project_url = Data.BASE_API_PATH + project + '/dashboard'
     response = requests.post(project_url,
                              headers=self.user_access_token,
                              json=Data.json_for_create_dashboard())
     content = ApiHelper.content(response)
     return content
Exemplo n.º 20
0
 def update_dashboard(self, dashboard_id):
     project = ApiHelper.get_project_name(self)
     project_url = Data.BASE_API_PATH + project + '/dashboard/' + dashboard_id
     response = requests.put(project_url,
                             headers=self.user_access_token,
                             json=Data.json_for_update_dashboard())
     content = ApiHelper.content(response)
     return content
Exemplo n.º 21
0
 def create_user(self, *args):
     content = []
     user_url = Data.BASE_API_PATH + '/user'
     if args:
         json = Data.json_for_create_user(args)
         response = requests.post(user_url,
                                  headers=self.admin_token,
                                  json=json)
     else:
         json = Data.json_for_create_user()
         response = requests.post(user_url,
                                  headers=self.admin_token,
                                  json=json)
     status_code = response.status_code
     content.append(status_code)
     response = response.json()
     content.append(response)
     content.append(json)
     return content
Exemplo n.º 22
0
class DataTest(TestCase, BaseTestCase):
    @classmethod
    def setUpClass(self):
        self.data = Data(self.sc, StubConfig)

    def test_main(self):
        # Test nearby restaurant detection
        nearby = self.data.nearby_restaurants(self.bookings)
        self.assertIsInstance(nearby, RDD)
        self.assertTrue(nearby.count() > 0)
        self.assertTrue(len(nearby.first()) == 2)

        # Test outlier filtering
        filtered = self.data.filter_outliers(self.bookings)
        self.assertIsInstance(filtered, DataFrame)
        self.assertTrue(filtered.count() < self.bookings.count())

    @classmethod
    def tearDownClass(self):
        del self.data
Exemplo n.º 23
0
 def __init__(self) -> None:
     super().__init__()
     # 加载UI
     self.ui = ui_main.Ui_MainWindow()
     self.ui.setupUi(self)
     # 加载数据
     self.data = Data(self)
     self.data.setItems()
     # 绑定槽函数
     ## 数据.添加项
     self.add_window = InfoEditor()
     self.ui.item_add.triggered.connect(self.add_window.show)  # 显示窗口
     self.add_window.ui.ok.clicked.connect(self.data.addItem)  # (窗口) 确认
     self.add_window.ui.close.clicked.connect(
         self.add_window.close)  # (窗口) 关闭
     ## 数据.删除项
     self.ui.item_remove.triggered.connect(self.data.rmItem)
     ## 编辑.信息
     self.change_window = InfoEditor()
     self.ui.item_change.triggered.connect(self.chitem_show)  # 显示窗口
     self.change_window.ui.ok.clicked.connect(self.data.chItem)  # (窗口) 确认
     self.change_window.ui.close.clicked.connect(
         self.change_window.close)  # (窗口) 关闭
     ## 编辑.内容
     self.markdown_editor = MarkdownEditor()
     self.ui.content.triggered.connect(self.markdown_editor_show)  # 显示窗口
     self.markdown_editor.ui.open.triggered.connect(
         self.markdown_editor_open)
     self.markdown_editor.ui.save.triggered.connect(
         self.markdown_editor_save)
     self.markdown_editor.ui.view.triggered.connect(
         self.markdown_editor_view)
     ## 查找.排序
     self.ui.sort.triggered.connect(self.item_sort)
     ## 查找.搜索
     self.search_window = FindEditor()
     self.ui.search.triggered.connect(self.search_window.show)
     self.search_window.ui.submit.clicked.connect(self.item_find)
Exemplo n.º 24
0
class SystemAlgorithmTest(TestCase, BaseTestCase):
    def setUp(self):
        self.alg = System(self.sc, config=StubConfig)
        self.data = Data(self.sc, config=StubConfig)
        self.maximum_weight = StubConfig.get('System', 'maximum_weight')

        # Replace all other recommenders with a
        # stub that likes every restaurant
        # This stub must be defined in default.cfg
        self.alg.recommenders = {'CuisineType': StubCuisineType(self.sc)}
        self.alg.weights = {'CuisineType': 1}

    def test01_predict(self):
        self.alg.train(self.bookings)
        # Check that predictions match interface
        self.assertIsInstance(
            self.alg.predict(self.data.nearby_restaurants(self.bookings)),
            DataFrame)

        # Check for detection of empty RDD
        self.assertRaises(ValueError, self.alg.predict,
                          self.sc.parallelize([]))

    def test02_weights(self):
        self.alg.recommenders = {"a": "a", "b": "b", "c": "c"}

        weights = self.alg.generate_weights(self.maximum_weight)

        for tpl in weights:
            # All coefficients are in [0, maximum_weight]
            for val in tpl:
                self.assertTrue(0 <= val <= self.maximum_weight)

            # Ensure tuples that are integer multiples
            # of other tuples are not present
            for tpl2 in weights:
                if tpl != tpl2:
                    self.assertFalse(collinear(tpl, tpl2))

    def test03_learn(self):
        self.alg.config.set_weights((1, 1, 1, 1))
        self.alg.learn_hyperparameters(self.bookings)

        # After learning, at least one recommender should have a
        # non-zero weight, i.e., the sum has to be positive
        self.assertTrue(sum(self.alg.config.get_weights()) > 0)

    def tearDown(self):
        del self.alg
        del self.data
Exemplo n.º 25
0
    def __init__(self):
        data = Data()

        def empty_check(dict):
            if dict:
                return False
            else:
                return True

        with open('final_data.txt', 'r+') as f:
            # set text inside to this var which is now a list of our data
            a_data = ast.literal_eval(f.read())
            a_data = [d for d in a_data if d]

        self.df = pd.DataFrame(a_data)
        self.df.dropna()
        self.df.isna()
Exemplo n.º 26
0
 def create_project(self, *args):
     content = []
     request_json = Data.json_for_create_new_project()
     project_url = Data.BASE_API_PATH + 'project'
     if args:
         response = requests.post(project_url,
                                  headers=self.admin_token,
                                  json=request_json)
     else:
         response = requests.post(project_url,
                                  headers=self.user_access_token,
                                  json=request_json)
     status_code = response.status_code
     content.append(status_code)
     response = response.json()
     content.append(response)
     content.append(request_json["projectName"])
     return content
Exemplo n.º 27
0
    def detect_type(self):
        for data in self.data_list:
            d = Data(data)
            if (d.my_type == 'ENC'):
                continue
            elif ('KEY' in d.my_type):
                d = Key(data)  # creation de la class Key
            elif ('CRT' in d.my_type):
                try:
                    # creation de la class Certificat avec PEM
                    d = Certificate(data, 'PEM')
                except:
                    continue

            elif ('DER' in d.my_type):
                try:
                    # creation de la class Certificat avec DER
                    d = Certificate(data, 'DER')
                except:
                    continue
            self.objects_list.append(d)
        return len(self.objects_list)
Exemplo n.º 28
0
import xgboost as xgb
import numpy as np
from sklearn.ensemble import RandomForestRegressor

import src.constants as const
from src.data import Data
from src.evaluation import Evaluator as eval
from src.params import Params
from src.visualization import Visualisation as visual

input_files = [
    f for f in listdir(const.data_path) if isfile(join(const.data_path, f))
]

data = Data(input_files, fill_strategy='gate')
params = Params()

visual.create_feature_map(data.lagged_feature_names)

#visual.plot2d(data, 'crude_oil')
#visual.plot_all_2d(data)

# Global variables
xgb_num_rounds = 80

td = np.array(data.train_data.drop(['output'], axis=1))

train_data = data.train_data.drop(['output'], axis=1).as_matrix()
train_label = data.train_data[['output']].as_matrix()
test_data = data.test_data.drop(['output'], axis=1).as_matrix()
Exemplo n.º 29
0
def main():
    tesla_filename = os.path.join(DATA_DIR, 'TSLA.csv')
    hitachy_filename = os.path.join(DATA_DIR, './HTHIY.csv')  # Hitachi, Ltd.
    ivr_filename = os.path.join(DATA_DIR,
                                './IYR.csv')  # iShares U.S. Real Estate ETF
    lithium_filename = os.path.join(
        DATA_DIR, './LIT.csv')  # Global X Lithium & Battery Tech ETF (LIT)

    # Read CSV files into Panda Dataframes
    r_y = Data(tesla_filename)
    y = r_y.to_panda('tesla')

    r_x1 = Data(hitachy_filename)
    x1 = r_x1.to_panda('hitachy')

    r_x2 = Data(lithium_filename)
    x2 = r_x2.to_panda('lithium')

    r_x3 = Data(ivr_filename)
    x3 = r_x3.to_panda('ivr')

    from functools import reduce
    data_frames = [
        x1.drop(['hitachy_price'], axis=1),
        x2.drop(['lithium_price'], axis=1),
        x3.drop(['ivr_price'], axis=1),
        y.drop(['tesla_price'], axis=1)
    ]

    df_merged = reduce(
        lambda left, right: pd.merge(left, right, on=['Date'], how='outer'),
        data_frames).fillna('void')

    # Get the correlation Matrix
    dt = DataTool(df_merged)
    dt.corr_matrix(OUT_DIR)

    #  Stack multiple independent variables, only keep the Returns columns
    x = np.vstack(
        (x1['hitachy_returns'], x2['lithium_returns'], x3['ivr_returns']))
    x = x.T
    y = y['tesla_returns']

    # Apply linear regression
    regression = Regression()
    model = regression.createModel()
    model.fit(x, y)
    model.score(x, y)  # R-square

    import statsmodels.api as sm
    # By default statsmodel does not include intercept for regression (A in : y =  A + B*x1 + C*x2 )
    X = sm.add_constant(x)
    # Create Ordinary Least-Squares (OLS) model
    sm_model = sm.OLS(y, X)
    fit = sm_model.fit()

    # Save summary in pickle
    fit.save(OUT_DIR + "/fit_summary.pickle")

    with open(OUT_DIR + "/fit_summary.txt", 'w') as f:
        print(fit.summary(), file=f)

    print(fit.summary())
Exemplo n.º 30
0
# -*- coding:UTF-8 -*-
"""
@author: ZhaoHe
"""

from src.data import Data
from src.config import dataset2_table_name

if __name__ == "__main__":
    data = Data()
    # 处理数据集的标称属性
    data.process_nom_features(data.dataset2_nom_feature_list, dataset2_table_name)
Exemplo n.º 31
0
from src import app, api
from flask_restful import Resource
from src.data import Data
from src.models import Pair, Interval

data = Data()

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

class Test(Resource):
    def get(self):
        return {'results':data.test()}

class History(Resource):
    def get(self, symbol_1:str, symbol_2:str, _datetime:int):
        pair = Pair(symbol_1, symbol_2)
        res = data.history(pair, _datetime)   
        return res
class HistoryRange(Resource):
    def get(self, symbol_1:str, symbol_2:str, datetime_start:int, datetime_end:int, interval:str):
        pair = Pair(symbol_1, symbol_2)
        interval = Interval(interval)
        res = data.history(pair, datetime_start, datetime_end, interval)   
        return res

api.add_resource(HelloWorld, '/')
api.add_resource(Test,'/test')
api.add_resource(History,'/history/<string:symbol_1>/<string:symbol_2>/<int:_datetime>')
api.add_resource(HistoryRange,'/history/<string:symbol_1>/<string:symbol_2>/<int:datetime_start>/<int:datetime_end>/<string:interval>')
Exemplo n.º 32
0
class Solver:
    def __init__(self, data):
        self.data = data
        self.initial_guess = [1, 1, 1, data.center[0], data.center[1]]

    # constraint function (x[0] == a, x[1] == b, x[2] == c, x[3] == d, x[4] == alpha, x[5] == beta)
    # def h(self, x, number):
    #     det = x[0] * x[3] - x[1] * x[2]
    #     if det == 0:
    #         return - 1  # 1 is more than 0 so the constraint does not hold
    #     else:
    #         return -((1 / det ** 2) * ((x[3] * self.data.df.iloc[0, number] - x[1] * (self.data.df.iloc[1, number]
    #                                                                                 - x[4] * (det ** 2))) ** 2
    #                                  + (x[0] * self.data.df.iloc[1, number] - x[2] * self.data.df.iloc[0, number]
    #                                     - x[5] * (det ** 2)) ** 2) - 1)

    def h(self, x, number):
        return 1 - (
            (x[0]**2 * self.data.new_df.iloc[0, number] +
             x[0] * x[2] * self.data.new_df.iloc[1, number] - x[3])**2 +
            (x[0] * x[2] * self.data.new_df.iloc[0, number] +
             (x[1]**2 + x[2]**2) * self.data.new_df.iloc[1, number] - x[4])**2)

    # variables used for finding out whether to discard the point
    point_cost = 10
    square_cost = 1  # cost of one m^2 of area
    square = 0.0  # ellipse area - target function value
    x = 0.0  # elements of S
    y = 0.0
    z = 0.0
    alpha = 0.0  # shift vector
    beta = 0.0

    vector = np.zeros(5)

    data = Data()  # Data object will be transferred here

    initial_guess = np.zeros(5)

    def set_fields(self, x, y, z, alpha, beta):
        # self.a = a
        # self.b = b
        # self.c = c
        # self.d = d
        # self.alpha = alpha
        # self.beta = beta
        # self.vector = [a, b, c, d, alpha, beta]
        self.x = x
        self.y = y
        self.z = z
        self.alpha = alpha
        self.beta = beta
        self.vector = [x, y, z, alpha, beta]

    def restrictions(
            self):  # counting all restrictions and assembling together
        cons = list()  # list of dictionaries
        h_list = list()  # list of constraints - functions h_i

        # number of restrictions == number of points left (columns in new_df)
        for i in range(len(self.data.new_df.columns)):
            h_list.append(lambda x: self.h(x, i))
            cons.append({
                'type': 'ineq',
                'fun': h_list[i]
            })  # appending each constraint as a dictionary

        return cons

    def optimize(self):  # computing matrix S and vector (alpha, beta)^T
        w = self.minimal_result()
        self.set_fields(w[0], w[1], w[2], w[3], w[4])
        current_square = f(w[0:3])  # latest calculated square
        #        print("\n" + "Starting square is " + str(current_square))
        self.data.discard_point(False)

        while True:

            self.square = current_square
            w = self.minimal_result()
            current_square = f(w[0:3])
            delta_square = self.square - current_square  # area change
            if delta_square * self.square_cost < self.point_cost:
                break
            self.set_fields(w[0], w[1], w[2], w[3], w[4])
            self.data.discard_point(False)

    # counting optimal values for points in new_df
    def minimal_result(self):
        result = minimize(f,
                          self.initial_guess,
                          constraints=self.restrictions())
        return result.x

    def display(self):
        # Let ellipse be  (x y)*Q*(x y)^T + L^T*(x y) + c
        # set edges for of displayed field
        edge = 40.0
        x_min = -edge
        x_max = edge
        y_min = -edge
        y_max = edge
        axes = plt.gca()
        axes.set_xlim([x_min, x_max])
        axes.set_ylim([y_min, y_max])

        x = np.linspace(-20.0, 20.0, 100)
        y = np.linspace(-20.0, 20.0, 100)
        xx, yy = np.meshgrid(x, y)

        # draw the ellipse
        # ellipse = ((self.a ** 2 + self.c ** 2) * xx) ** 2 + 2 * (self.a * self.b + self.c * self.d) * xx * yy \
        #           + ((self.b ** 2 + self.d ** 2) * yy) ** 2 - 2 * (self.alpha * self.a + self.beta * self.c) * xx \
        #           - 2 * (self.alpha * self.b + self.beta * self.d) * yy + self.alpha ** 2 + self.beta ** 2 - 1

        ellipse = 0

        plt.contour(xx, yy, ellipse, [0])

        # just draw points
        for i in range(len(self.data.df.columns)):
            plt.plot(self.data.df.iloc[0, i], self.data.df.iloc[1, i], 'bo')
        plt.show()