Skip to content

Commit

Permalink
fix_pylint_for_CI (microsoft#1119)
Browse files Browse the repository at this point in the history
* fix_pylint_for_CI

* reformat_with_black

* fix_pylint_C3001

* fix_flake8_error
  • Loading branch information
SunsetWolf authored Jun 9, 2022
1 parent b4560e3 commit eb4f4bc
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
run: |
pip install --upgrade pip
pip install pylint
pylint --disable=C0104,C0114,C0115,C0116,C0301,C0302,C0411,C0413,C1802,R0201,R0401,R0801,R0902,R0903,R0911,R0912,R0913,R0914,R0915,R1720,W0105,W0123,W0201,W0511,W0613,W1113,W1514,E0401,E1121,C0103,C0209,R0402,R1705,R1710,R1725,R1735,W0102,W0212,W0221,W0223,W0231,W0237,W0612,W0621,W0622,W0703,W1309,E1102,E1136 --const-rgx='[a-z_][a-z0-9_]{2,30}$' qlib --init-hook "import astroid; astroid.context.InferenceContext.max_inferred = 500"
pylint --disable=C0104,C0114,C0115,C0116,C0301,C0302,C0411,C0413,C1802,R0401,R0801,R0902,R0903,R0911,R0912,R0913,R0914,R0915,R1720,W0105,W0123,W0201,W0511,W0613,W1113,W1514,E0401,E1121,C0103,C0209,R0402,R1705,R1710,R1725,R1735,W0102,W0212,W0221,W0223,W0231,W0237,W0612,W0621,W0622,W0703,W1309,E1102,E1136 --const-rgx='[a-z_][a-z0-9_]{2,30}$' qlib --init-hook "import astroid; astroid.context.InferenceContext.max_inferred = 500"
# The following flake8 error codes were ignored:
# E501 line too long
Expand Down
15 changes: 12 additions & 3 deletions qlib/contrib/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@ def _get_date_parse_fn(target):
get_date_parse_fn(20120101)('2017-01-01') => 20170101
"""
if isinstance(target, int):
_fn = lambda x: int(str(x).replace("-", "")[:8]) # 20200201

def _fn(x):
return int(str(x).replace("-", "")[:8]) # 20200201

elif isinstance(target, str) and len(target) == 8:
_fn = lambda x: str(x).replace("-", "")[:8] # '20200201'

def _fn(x):
return str(x).replace("-", "")[:8] # '20200201'

else:
_fn = lambda x: x # '2021-01-01'

def _fn(x):
return x # '2021-01-01'

return _fn


Expand Down
5 changes: 4 additions & 1 deletion qlib/contrib/data/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ def parse_config_to_fields(config):
exclude = config["rolling"].get("exclude", [])
# `exclude` in dataset config unnecessary filed
# `include` in dataset config necessary field
use = lambda x: x not in exclude and (include is None or x in include)

def use(x):
return x not in exclude and (include is None or x in include)

if use("ROC"):
fields += ["Ref($close, %d)/$close" % d for d in windows]
names += ["ROC%d" % d for d in windows]
Expand Down
9 changes: 7 additions & 2 deletions qlib/contrib/eva/alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def calc_long_short_prec(

group = df.groupby(level=date_col)

N = lambda x: int(len(x) * quantile)
def N(x):
return int(len(x) * quantile)

# find the top/low quantile of prediction and treat them as long and short target
long = group.apply(lambda x: x.nlargest(N(x), columns="pred").label).reset_index(level=0, drop=True)
short = group.apply(lambda x: x.nsmallest(N(x), columns="pred").label).reset_index(level=0, drop=True)
Expand Down Expand Up @@ -98,7 +100,10 @@ def calc_long_short_return(
if dropna:
df.dropna(inplace=True)
group = df.groupby(level=date_col)
N = lambda x: int(len(x) * quantile)

def N(x):
return int(len(x) * quantile)

r_long = group.apply(lambda x: x.nlargest(N(x), columns="pred").label.mean())
r_short = group.apply(lambda x: x.nsmallest(N(x), columns="pred").label.mean())
r_avg = group.label.mean()
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/meta/data_selection/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def _prepare_meta_ipt(self, task):
ic_df = self.internal_data.data_ic_df

segs = task["dataset"]["kwargs"]["segments"]
end = max([segs[k][1] for k in ("train", "valid") if k in segs])
end = max(segs[k][1] for k in ("train", "valid") if k in segs)
ic_df_avail = ic_df.loc[:end, pd.IndexSlice[:, :end]]

# meta data set focus on the **information** instead of preprocess
Expand Down
5 changes: 4 additions & 1 deletion qlib/contrib/model/highfreq_gdbt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ def _prepare_data(self, dataset: DatasetH):
# Convert label into alpha
df_train["label"][l_name] = df_train["label"][l_name] - df_train["label"][l_name].mean(level=0)
df_valid["label"][l_name] = df_valid["label"][l_name] - df_valid["label"][l_name].mean(level=0)
mapping_fn = lambda x: 0 if x < 0 else 1

def mapping_fn(x):
return 0 if x < 0 else 1

df_train["label_c"] = df_train["label"][l_name].apply(mapping_fn)
df_valid["label_c"] = df_valid["label"][l_name].apply(mapping_fn)
x_train, y_train = df_train["feature"], df_train["label_c"].values
Expand Down
4 changes: 3 additions & 1 deletion qlib/contrib/model/pytorch_hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ def fit(
pretrained_model.load_state_dict(torch.load(self.model_path))

model_dict = self.HIST_model.state_dict()
pretrained_dict = {k: v for k, v in pretrained_model.state_dict().items() if k in model_dict}
pretrained_dict = {
k: v for k, v in pretrained_model.state_dict().items() if k in model_dict # pylint: disable=E1135
}
model_dict.update(pretrained_dict)
self.HIST_model.load_state_dict(model_dict)
self.logger.info("Loading pretrained model Done...")
Expand Down
4 changes: 2 additions & 2 deletions qlib/contrib/model/pytorch_tra.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def _init_model(self):
for param in self.tra.predictors.parameters():
param.requires_grad_(False)

self.logger.info("# model params: %d" % sum([p.numel() for p in self.model.parameters() if p.requires_grad]))
self.logger.info("# tra params: %d" % sum([p.numel() for p in self.tra.parameters() if p.requires_grad]))
self.logger.info("# model params: %d" % sum(p.numel() for p in self.model.parameters() if p.requires_grad))
self.logger.info("# tra params: %d" % sum(p.numel() for p in self.tra.parameters() if p.requires_grad))

self.optimizer = optim.Adam(list(self.model.parameters()) + list(self.tra.parameters()), lr=self.lr)

Expand Down
2 changes: 1 addition & 1 deletion qlib/data/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def build_index(data: pd.DataFrame) -> Tuple[pd.DataFrame, dict]:

@property
def empty(self):
return self.__len__() == 0
return len(self) == 0

def _get_indices(self, row: int, col: int) -> np.array:
"""
Expand Down
2 changes: 1 addition & 1 deletion qlib/rl/utils/data_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __del__(self):
def __iter__(self):
if not self._activated:
raise ValueError(
"Need to call activate() to launch a daemon worker " "to produce data into data queue before using it."
"Need to call activate() to launch a daemon worker to produce data into data queue before using it."
)
return self._consumer()

Expand Down
5 changes: 4 additions & 1 deletion qlib/workflow/task/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ def __init__(
self.experiment = experiment
self.artifacts_path = artifacts_path
if rec_key_func is None:
rec_key_func = lambda rec: rec.info["id"]

def rec_key_func(rec):
return rec.info["id"]

if artifacts_key is None:
artifacts_key = list(self.artifacts_path.keys())
self.rec_key_func = rec_key_func
Expand Down

0 comments on commit eb4f4bc

Please sign in to comment.