Skip to content

Commit

Permalink
Add isort precommit
Browse files Browse the repository at this point in the history
precommit에 isort도 추가하였습니다.
  • Loading branch information
ehddnr301 committed Oct 6, 2021
1 parent 6af118b commit 7ef78f0
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ repos:
hooks:
- id: black
language_version: python3
- repo: https://github.com/PyCQA/isort
rev: 5.6.4
hooks:
- id: isort
language_version: python3
6 changes: 3 additions & 3 deletions app/api/router/predict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from typing import List


import numpy as np
from fastapi import APIRouter
from starlette.concurrency import run_in_threadpool
Expand All @@ -12,12 +11,13 @@
from app.utils import ScikitLearnModel, my_model
from logger import L


models.Base.metadata.create_all(bind=engine)


router = APIRouter(
prefix="/predict", tags=["predict"], responses={404: {"description": "Not Found"}}
prefix="/predict",
tags=["predict"],
responses={404: {"description": "Not Found"}},
)


Expand Down
26 changes: 20 additions & 6 deletions app/api/router/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import re
import subprocess


from fastapi import APIRouter

from app.utils import NniWatcher, ExperimentOwl, base_dir, get_free_port, write_yml
from app.utils import (
ExperimentOwl,
NniWatcher,
base_dir,
get_free_port,
write_yml,
)
from logger import L

router = APIRouter(
prefix="/train", tags=["train"], responses={404: {"description": "Not Found"}}
prefix="/train",
tags=["train"],
responses={404: {"description": "Not Found"}},
)


Expand Down Expand Up @@ -44,14 +51,18 @@ def train_insurance(
try:
write_yml(path, experiment_name, experimenter, model_name, version)
nni_create_result = subprocess.getoutput(
"nnictl create --port {} --config {}/{}.yml".format(PORT, path, model_name)
"nnictl create --port {} --config {}/{}.yml".format(
PORT, path, model_name
)
)
sucs_msg = "Successfully started experiment!"

if sucs_msg in nni_create_result:
p = re.compile(r"The experiment id is ([a-zA-Z0-9]+)\n")
expr_id = p.findall(nni_create_result)[0]
nni_watcher = NniWatcher(expr_id, experiment_name, experimenter, version)
nni_watcher = NniWatcher(
expr_id, experiment_name, experimenter, version
)
m_process = multiprocessing.Process(target=nni_watcher.excute)
m_process.start()

Expand All @@ -68,6 +79,7 @@ def train_atmos(expr_name: str):
"""
온도 시계열과 관련된 학습을 실행하기 위한 API입니다.
Args:
expr_name(str): NNI가 실행할 실험의 이름 입니다. 이 파라미터를 기반으로 project_dir/experiments/[expr_name] 경로로 찾아가 config.yml을 이용하여 NNI를 실행합니다.
Expand All @@ -83,7 +95,9 @@ def train_atmos(expr_name: str):

try:
nni_create_result = subprocess.getoutput(
"nnictl create --port {} --config {}/config.yml".format(nni_port, expr_path)
"nnictl create --port {} --config {}/config.yml".format(
nni_port, expr_path
)
)
sucs_msg = "Successfully started experiment!"

Expand Down
3 changes: 1 addition & 2 deletions app/database.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os


from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

load_dotenv(verbose=True)

Expand Down
13 changes: 10 additions & 3 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# -*- coding: utf-8 -*-
import datetime


from sqlalchemy import Column, Integer, String, FLOAT, DateTime, ForeignKey, LargeBinary
from sqlalchemy.sql.functions import now
from sqlalchemy import (
FLOAT,
Column,
DateTime,
ForeignKey,
Integer,
LargeBinary,
String,
)
from sqlalchemy.orm import relationship
from sqlalchemy.sql.functions import now

from app.database import Base

Expand Down
2 changes: 1 addition & 1 deletion app/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@
WHERE NOT EXISTS (SELECT 1
FROM atmos_model_metadata as amm
WHERE amm.model_name = '{mn}');
"""
"""
45 changes: 34 additions & 11 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import time
import zipfile


import tensorflow as tf
import yaml

Expand Down Expand Up @@ -239,7 +238,9 @@ def watch_process(self):
if self.is_kill:
while True:
self.get_running_experiment()
if self._running_experiment and ("DONE" in self._running_experiment[0]):
if self._running_experiment and (
"DONE" in self._running_experiment[0]
):
_stop_expr = subprocess.getoutput(
"nnictl stop {}".format(self.experiment_id)
)
Expand Down Expand Up @@ -284,7 +285,9 @@ def model_final_update(self):

if saved_result is None:
engine.execute(
INSERT_MODEL_CORE.format(final_result.model_name, pickled_model)
INSERT_MODEL_CORE.format(
final_result.model_name, pickled_model
)
)
engine.execute(
INSERT_MODEL_METADATA.format(
Expand All @@ -303,7 +306,9 @@ def model_final_update(self):
> final_result[self.evaluation_criteria]
):
engine.execute(
UPDATE_MODEL_CORE.format(pickled_model, saved_result.model_name)
UPDATE_MODEL_CORE.format(
pickled_model, saved_result.model_name
)
)
engine.execute(
UPDATE_MODEL_METADATA.format(
Expand All @@ -315,7 +320,9 @@ def model_final_update(self):
)
)

engine.execute(DELETE_ALL_EXPERIMENTS_BY_EXPR_NAME.format(self.experiment_name))
engine.execute(
DELETE_ALL_EXPERIMENTS_BY_EXPR_NAME.format(self.experiment_name)
)


def zip_model(model_path):
Expand Down Expand Up @@ -401,7 +408,12 @@ class ExperimentOwl:
"""

def __init__(
self, experiment_id, experiment_name, experiment_path, mfile_manage=True, time=5
self,
experiment_id,
experiment_name,
experiment_path,
mfile_manage=True,
time=5,
):
self.__minute = 60
self.time = time * self.__minute
Expand Down Expand Up @@ -434,7 +446,9 @@ def main(self):
expr_list = subprocess.getoutput("nnictl experiment list")

running_expr = [
expr for expr in expr_list.split("\n") if self.experiment_id in expr
expr
for expr in expr_list.split("\n")
if self.experiment_id in expr
]
print(running_expr)
if running_expr and ("DONE" in running_expr[0]):
Expand Down Expand Up @@ -486,17 +500,23 @@ def update_tfmodeldb(self):

if not saved_score or (metrics[0] < saved_score[0]):
winner_model = os.path.join(
os.path.join(self.experiment_path, "temp", self.experiment_name)
os.path.join(
self.experiment_path, "temp", self.experiment_name
)
)
if os.path.exists:
shutil.rmtree(winner_model)
os.rename(exprs, winner_model)

m_buffer = zip_model(winner_model)
encode_model = codecs.encode(pickle.dumps(m_buffer), "base64").decode()
encode_model = codecs.encode(
pickle.dumps(m_buffer), "base64"
).decode()

engine.execute(
INSERT_OR_UPDATE_MODEL.format(mn=self.experiment_name, mf=encode_model)
INSERT_OR_UPDATE_MODEL.format(
mn=self.experiment_name, mf=encode_model
)
)
engine.execute(
INSERT_OR_UPDATE_SCORE.format(
Expand All @@ -506,7 +526,10 @@ def update_tfmodeldb(self):
score2=metrics[1],
)
)
L.info("saved model %s %s" % (self.experiment_id, self.experiment_name))
L.info(
"saved model %s %s"
% (self.experiment_id, self.experiment_name)
)

def modelfile_cleaner(self):
"""
Expand Down
18 changes: 9 additions & 9 deletions experiments/atmos_tmp_01/train.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import os
import sys
import time

from preprocessing import preprocess

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import numpy as np
import nni
import numpy as np
import pandas as pd
import tensorflow as tf
from expr_db import connect
from sklearn.metrics import mean_absolute_error, mean_squared_error
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.layers import GRU
from sklearn.metrics import mean_absolute_error, mean_squared_error


from expr_db import connect
from tensorflow.keras.layers import GRU, Dense
from tensorflow.keras.models import Sequential

physical_devices = tf.config.list_physical_devices("GPU")
if physical_devices:
Expand All @@ -32,7 +30,9 @@ def make_dataset(data, label, window_size=365, predsize=None):
for i in range(len(data) - (window_size + predsize)):
feature_list.append(np.array(data.iloc[i : i + window_size]))
label_list.append(
np.array(label.iloc[i + window_size : i + window_size + predsize])
np.array(
label.iloc[i + window_size : i + window_size + predsize]
)
)
else:
for i in range(len(data) - window_size):
Expand Down
3 changes: 2 additions & 1 deletion experiments/expr_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from dotenv import load_dotenv

import sqlalchemy
from dotenv import load_dotenv


def connect(db="postgres"):
Expand Down
12 changes: 9 additions & 3 deletions experiments/insurance/trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pickle
import sys


sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


Expand Down Expand Up @@ -41,7 +40,9 @@ def preprocess(x_train, x_valid, col_list):
encoder = LabelEncoder()

for col in col_list:
tmp_x_train.loc[:, col] = encoder.fit_transform(tmp_x_train.loc[:, col])
tmp_x_train.loc[:, col] = encoder.fit_transform(
tmp_x_train.loc[:, col]
)
tmp_x_valid.loc[:, col] = encoder.transform(tmp_x_valid.loc[:, col])

return tmp_x_train.values, tmp_x_valid.values
Expand Down Expand Up @@ -87,7 +88,12 @@ def main(params, engine, experiment_info, connection):
model = XGBRegressor(**params)

# 모델 학습 및 Early Stopping 적용
model.fit(x_tra, y_train, eval_set=[(x_val, y_valid)], early_stopping_rounds=10)
model.fit(
x_tra,
y_train,
eval_set=[(x_val, y_valid)],
early_stopping_rounds=10,
)

y_train_pred = model.predict(x_tra)
y_valid_pred = model.predict(x_val)
Expand Down
2 changes: 1 addition & 1 deletion logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import logging.handlers
from colorlog import ColoredFormatter

from colorlog import ColoredFormatter

L = logging.getLogger("snowdeer_log")
L.setLevel(logging.DEBUG)
Expand Down
9 changes: 6 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn


from app.api.router import predict, train

Expand All @@ -28,5 +27,9 @@ def hello_world():

if __name__ == "__main__":
uvicorn.run(
"main:app", host="0.0.0.0", port=8000, reload=True, reload_dirs=["app/"]
"main:app",
host="0.0.0.0",
port=8000,
reload=True,
reload_dirs=["app/"],
)
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.isort]
multi_line_output=3
include_trailing_comma=true
force_grid_wrap=0
use_parentheses=true
line_length=79

[tool.black]
line-length = 79
target-version = ['py38']

0 comments on commit 7ef78f0

Please sign in to comment.