Exemplo n.º 1
0
 def disconnect(self):
     if self.connection:
         if self.cursor:
             self.cursor.close()
         self.connection.close()
         logger.info("MYSQL connection is closed")
     self.connection = None
     self.cursor = None
Exemplo n.º 2
0
 def test_http_methods(self):
     logger.info("Testing Http methods like POST, PUT, DELETE")
     result = self.simulate_post('/api/v1/embl/search?name=tbx')
     self.assertEqual(result.status, falcon.HTTP_405)
     result = self.simulate_put('/api/v1/embl/search?name=tbx')
     self.assertEqual(result.status, falcon.HTTP_405)
     result = self.simulate_delete('/api/v1/embl/search?name=tbx')
     self.assertEqual(result.status, falcon.HTTP_405)
Exemplo n.º 3
0
 def test_validate_params(self):
     logger.info("Validating parameters")
     output = {
         u"error":
         u"Search param 'name' cannot be none or less than 3 chars"
     }
     result = self.simulate_get('/api/v1/embl/search?name=tb')
     self.assertEqual(result.json, output)
Exemplo n.º 4
0
 def send_mail(email_id, attach_file, to_email, book_title, book_author):
     if gk7.SEND_MAIL_TYPE == 'api':
         data = {
             'email_id': email_id,
             'file_path': attach_file,
             'to_user': to_email,
             'title': book_title,
             'author': book_author
         }
         ApiTask.post.delay(gk7.API_URL.get('mail'), urllib.urlencode(data))
         logger.info(u'调用API发送邮件接口中...')
         return True
     # 其他类型
     # 发送邮件并修改待发送邮件状态
     MailTask.send.delay(email_id, attach_file, to_email, book_title, book_author)
     logger.info(u'发送邮件中...')
     return True
Exemplo n.º 5
0
 def get_data(self, query, params, retry_counter=0) -> []:
     try:
         logger.info("running query " + query)
         self.cursor.execute(query, params)
         retry_counter = 0
     except DBError as error:
         if retry_counter >= Constants.limit_retries.value:
             raise error
         else:
             retry_counter += 1
             logger.info("DB Connection error {}. retrying {}".format(
                 str(error).strip(), retry_counter))
             time.sleep(2)
             self.reset()
             self.get_data(query, retry_counter)
     columns = tuple([col[0] for col in self.cursor.description])
     data = [dict(zip(columns, row)) for row in self.cursor]
     return data
Exemplo n.º 6
0
 def on_get(self, req, resp):
     gene_name = req.get_param(Constants.name.value, None)
     species = req.get_param(Constants.species.value, None)
     http_status = falcon.HTTP_400
     if gene_name and len(gene_name) >= Constants.min_keyword_count.value:
         query = """select  stable_id as id, display_label as name, species from gene_autocomplete where display_label like %s"""
         params = (gene_name.lower()+"%",)
         if species:
             query+=" and species = %s"
             params=(gene_name.lower()+"%",species,)
         try:
             http_status = falcon.HTTP_200
             response_body = json.dumps(self.database.get_data(query,params), ensure_ascii=False)
         except Exception as exp:
             logger.info(str(exp))
             http_status = falcon.HTTP_400
             response_body = json.dumps({Constants.error.value: str(exp)})
     else:
         response_body = json.dumps({Constants.error.value: "Search param 'name' cannot be none or less than 3 chars"})
     resp.content_type = falcon.MEDIA_JSON
     resp.status = http_status
     resp.body = response_body
Exemplo n.º 7
0
 def connect(self, retry_counter=0):
     if not self.connection:
         try:
             self.connection = connector.connect(
                 host=Constants.host.value,
                 user=Constants.user.value,
                 database=Constants.database.value,
                 pool_size=Constants.pool_size.value,
                 connect_timeout=3)
             retry_counter = 0
             return self.connection
         except DBError as error:
             if not self.reconnect or retry_counter >= Constants.limit_retries.value:
                 raise error
             else:
                 retry_counter += 1
                 logger.info(
                     "DB Connection error {}. reconnecting {}".format(
                         str(error).strip(), retry_counter))
                 time.sleep(5)
                 self.connect(retry_counter)
         except (Exception, DBError) as error:
             raise error
Exemplo n.º 8
0
    def labeling(self, close_df, num_days=5, save=False, min_pct=0.05):
        """labeling func

        ラベリングの処理を実行する

        Args:
            close_df(pandas.DataFrame): 価格情報
            num_days(int): xxx
            save(bool): 保存するかどうか
            min_pct(float): drop_labels で使用

        Note:
            1. 変化分が大きいシグナルを検出(t_events, get_t_events)、CUSUMサンプリング
            2. 変化率を計算(daily_vol_df, get_daily_vol)
            3. トリプルバリア法により、ラベルづけ可能なシグナルを検出(events, get_events)
            4. ラベルを決定(get_bin)
            5. 重複ラベルを削除(drop_labels)

        """
        logger.info("close_df len: {}".format(len(close_df)))
        t_events = get_t_events(close_df, h=30)
        daily_vol_df = self.get_daily_vol(close_df, num_days=num_days)
        logger.info("t_events len: {}".format(len(t_events)))

        events = self.get_events(close = close_df, t_events=t_events, ptsl=[0.1, 0.1], \
                                 trgt=daily_vol_df, min_ret=0.03, num_threads=1, num_days=num_days,\
                                 t1=True)
        logger.info("events len: {}".format(len(events)))
        bins = self.get_bins(events, close_df)
        bins = self.drop_labels(bins, min_pct=min_pct)

        if save:
            print("label file を保存します。")
            bins.to_csv(self.bins_save_filename)
            events.to_csv(self.events_save_filename)

        self.close_df = close_df
        self.bins = bins
        self.events = events

        return bins, events
Exemplo n.º 9
0
# 设置系统编码
reload(sys)
sys.setdefaultencoding('utf-8')


urls = (
    '/','Index',
    '/send', 'Send',
    '/admin/login', 'Login',
)
#web.config.debug = False
app = web.application(urls, globals())

class Index:

    def GET(self):
        return json.dumps({'msg':'not services'})

    def POST(self):
        return json.dumps({'msg':'not servicindex'})

if __name__ == "__main__":
    logger.info(u'-----------系统启动-----------')

    # 初始化书籍封面目录
    if not os.path.exists(gk7.BOOK_COVER_DIRS):
        os.makedirs(gk7.BOOK_COVER_DIRS)

    #web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)   ##这行是新增的
    app.run()
Exemplo n.º 10
0
    def sample_weighting(self, check_mc=False, save=False):
        """sample_weighting func

        標本の重み付けの処理

        Args:
            check_mc(bool): モンテカルロ法で逐次ブートストラップ法の有用性を確認するかどうか

        """

        # スニペット 4.1
        # スニペット 4.2 ラベルの平均性の推定
        num_co_events = mp_pandas_obj(func=self.mp_num_co_events,
                                      pd_obj=("molecule", self.events.index),
                                      num_threads=self.num_threads,
                                      close_idx=self.labeling.close_df.index,
                                      t1=self.events["t1"])

        num_co_events = num_co_events.loc[~num_co_events.index.duplicated(
            keep="last")]
        num_co_events = num_co_events.reindex(
            self.labeling.close_df.index).fillna(0)

        self.bins["tw"] = mp_pandas_obj(self.mp_sample_tw, ("molecule", self.events.index), num_threads=self.num_threads, \
                                        t1=self.events["t1"], num_co_events=num_co_events)

        # スニペット 4.10 絶対リターンの帰属による標本ウェイトの決定
        self.bins["w"] = mp_pandas_obj(self.mp_sample_w, ("molecule", self.events.index), num_threads=self.num_threads, \
                                       t1=self.events["t1"], num_co_events=num_co_events, close = self.labeling.close_df)
        self.bins["w"] *= self.bins.shape[0] / self.bins["w"].sum()

        # スニペット 4.11 時間減衰ファクターの実装
        self.bins["tw"] = get_time_decay(self.bins["tw"], c_if_last_w=0.5)

        # bar_ix = self.get_bar_ix()
        bar_ix = self.labeling.close_df.index
        if check_mc:
            # スニペット 4.8 標準的ブートストラップと逐次ブートストラップによる独自性
            num_iters = 2
            jobs = []
            for i in range(int(num_iters)):
                job = {
                    "func": SampleWeighting.aux_mc,
                    "t1": self.events["t1"],
                    "bar_ix": bar_ix
                }
                jobs.append(job)

            if self.num_threads == 1:
                out = process_jobs_single(jobs)

            out_df = pd.DataFrame(out)
            print("out_df", out_df)
            print("out_df describe", out_df.describe())

        # スニペット 4.3
        # スニペット 4.4
        ind_matrix = SampleWeighting.get_ind_matrix(bar_ix, self.events["t1"])
        phi = SampleWeighting.seq_bootstrap(ind_matrix)

        phi = list(set(sorted(phi)))
        self.bins_sampled = self.bins.iloc[phi, :]
        logger.info("サンプル前 -> サンプル後: {} -> {}".format(len(self.bins),
                                                      len(self.bins_sampled)))

        if save:
            print("結果を保存します")
            self.bins_sampled.to_csv(self.bins_save_filename)

        return self.bins_sampled
Exemplo n.º 11
0
 def process_request(self, req, resp):
     if req.method not in ["GET"]:
         logger.info("")
         resp.status = falcon.HTTP_405
         resp.body = json.dumps(
             {Constants.error.value: "API supports only [GET,]"})
Exemplo n.º 12
0

# 设置系统编码
reload(sys)
sys.setdefaultencoding('utf-8')


urls = (
    '/','Index',
    '/send', 'Send',
    '/admin/login', 'Login',
)
#web.config.debug = False
app = web.application(urls, globals())    

class Index:

    def GET(self):
        return json.dumps({'msg':'not services'})

    def POST(self):
        return json.dumps({'msg':'not servicindex'})

if __name__ == "__main__":
    logger.info(u'-----------系统启动-----------')

    #web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)   ##这行是新增的
    app.run()

#application = app.wsgifunc()
Exemplo n.º 13
0
 def test_get_all_records(self):
     logger.info("Searching for gene name tbx")
     result = self.simulate_get('/api/v1/embl/search?name=tbx')
     self.assertEqual(result.status, falcon.HTTP_200)